| 1 | /* |
| 2 | This Software is distributed with the following X11 License, |
| 3 | sometimes also known as MIT license. |
| 4 | |
| 5 | Copyright (c) 2010 Miguel A. Ballicora |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person |
| 8 | obtaining a copy of this software and associated documentation |
| 9 | files (the "Software"), to deal in the Software without |
| 10 | restriction, including without limitation the rights to use, |
| 11 | copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | copies of the Software, and to permit persons to whom the |
| 13 | Software is furnished to do so, subject to the following |
| 14 | conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be |
| 17 | included in all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | OTHER DEALINGS IN THE SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | /* NBBOTF will remove the internal bitbase on the fly */ |
| 31 | #ifdef NBBOTF |
| 32 | #ifdef WDL_PROBE |
| 33 | #undef WDL_PROBE |
| 34 | #endif |
| 35 | #else |
| 36 | #define WDL_PROBE |
| 37 | #endif |
| 38 | |
| 39 | /*-- Intended to be modified to make public --> Supporting functions the TB generator ---------------------*/ |
| 40 | |
| 41 | #ifdef GTB_SHARE |
| 42 | #define SHARED_forbuilding |
| 43 | #endif |
| 44 | |
| 45 | /*---------------------------------------------------------------------------------------------------------*/ |
| 46 | #include <stdlib.h> |
| 47 | #include <stdio.h> |
| 48 | #include <string.h> |
| 49 | |
| 50 | #include "gtb-probe.h" |
| 51 | |
| 52 | #if defined(SHARED_forbuilding) |
| 53 | #include "gtb-prob2.h" |
| 54 | #else |
| 55 | #define mySHARED static |
| 56 | typedef unsigned char SQ_CONTENT; |
| 57 | typedef unsigned int SQUARE; |
| 58 | #endif |
| 59 | |
| 60 | #include "sysport.h" |
| 61 | #include "gtb-att.h" |
| 62 | #include "gtb-types.h" |
| 63 | |
| 64 | /*---------------------------------------------------------------------------------------------------------*/ |
| 65 | /*#include "posit_t.h"*/ |
| 66 | |
| 67 | #define MAX_LISTSIZE 17 |
| 68 | #if 0 |
| 69 | typedef unsigned sq_t; |
| 70 | typedef unsigned char pc_t; |
| 71 | typedef uint32_t mv_t; |
| 72 | #endif |
| 73 | |
| 74 | struct posit { |
| 75 | sq_t ws[MAX_LISTSIZE]; |
| 76 | sq_t bs[MAX_LISTSIZE]; |
| 77 | pc_t wp[MAX_LISTSIZE]; |
| 78 | pc_t bp[MAX_LISTSIZE]; |
| 79 | sq_t ep; |
| 80 | unsigned int stm; |
| 81 | unsigned int cas; |
| 82 | }; |
| 83 | typedef struct posit posit_t; |
| 84 | |
| 85 | #if 0 |
| 86 | typedef long int tbkey_t; |
| 87 | #endif |
| 88 | |
| 89 | /*---------------------------------------------------------------------------------------------------------*/ |
| 90 | /*#include "bool_t.h"*/ |
| 91 | |
| 92 | #if !defined(H_BOOL) |
| 93 | typedef int bool_t; |
| 94 | #endif |
| 95 | |
| 96 | #if !defined(TRUE) |
| 97 | #define TRUE ((bool_t)1) |
| 98 | #endif |
| 99 | |
| 100 | #if !defined(FALSE) |
| 101 | #define FALSE ((bool_t)0) |
| 102 | #endif |
| 103 | |
| 104 | /*--------- private if external building code is not present ----------------------------------------------*/ |
| 105 | |
| 106 | #if !defined(SHARED_forbuilding) |
| 107 | |
| 108 | #define MAX_EGKEYS 145 |
| 109 | #define SLOTSIZE 1 |
| 110 | #define NOINDEX ((index_t)(-1)) |
| 111 | |
| 112 | #if 0 |
| 113 | typedef unsigned short int dtm_t; |
| 114 | typedef size_t index_t; |
| 115 | /*typedef int index_t;*/ |
| 116 | #endif |
| 117 | |
| 118 | enum Loading_status { |
| 119 | STATUS_ABSENT = 0, |
| 120 | STATUS_STATICRAM = 1, |
| 121 | STATUS_MALLOC = 2, |
| 122 | STATUS_FILE = 3, |
| 123 | STATUS_REJECT = 4 |
| 124 | }; |
| 125 | |
| 126 | struct endgamekey { |
| 127 | int id; |
| 128 | const char *str; |
| 129 | index_t maxindex; |
| 130 | index_t slice_n; |
| 131 | void (*itopc) (index_t, SQUARE *, SQUARE *); |
| 132 | bool_t (*pctoi) (const SQUARE *, const SQUARE *, index_t *); |
| 133 | dtm_t * egt_w; |
| 134 | dtm_t * egt_b; |
| 135 | FILE * fd; |
| 136 | int status; |
| 137 | int pathn; |
| 138 | }; |
| 139 | #endif |
| 140 | |
| 141 | /*----------------------------------------------------------------------------------------------------------*/ |
| 142 | |
| 143 | /* array for better moves */ |
| 144 | #ifdef GTB_SHARE |
| 145 | mySHARED int bettarr [2] [8] [8]; |
| 146 | #endif |
| 147 | |
| 148 | /*------------ ENUMS ----------------------------------------------------------*/ |
| 149 | |
| 150 | enum Mask_values { |
| 151 | RESMASK = tb_RESMASK, |
| 152 | INFOMASK = tb_INFOMASK, |
| 153 | PLYSHIFT = tb_PLYSHIFT |
| 154 | }; |
| 155 | |
| 156 | enum Info_values { |
| 157 | iDRAW = tb_DRAW, |
| 158 | iWMATE = tb_WMATE, |
| 159 | iBMATE = tb_BMATE, |
| 160 | iFORBID = tb_FORBID, |
| 161 | |
| 162 | iDRAWt = tb_DRAW |4, |
| 163 | iWMATEt = tb_WMATE |4, |
| 164 | iBMATEt = tb_BMATE |4, |
| 165 | iUNKNOWN = tb_UNKNOWN, |
| 166 | |
| 167 | iUNKNBIT = (1<<2) |
| 168 | }; |
| 169 | |
| 170 | /*-------------------------- inherited from a previous maindef.h -----------*/ |
| 171 | |
| 172 | #define WHITES (1u<<6) |
| 173 | #define BLACKS (1u<<7) |
| 174 | |
| 175 | #define NOPIECE 0u |
| 176 | #define PAWN 1u |
| 177 | #define KNIGHT 2u |
| 178 | #define BISHOP 3u |
| 179 | #define ROOK 4u |
| 180 | #define QUEEN 5u |
| 181 | #define KING 6u |
| 182 | |
| 183 | #define WH 0 |
| 184 | #define BL 1 |
| 185 | #define Opp(x) ((x)^1) |
| 186 | #define wK (KING | WHITES) |
| 187 | |
| 188 | /*------------------- |
| 189 | SQUARES |
| 190 | -------------------*/ |
| 191 | |
| 192 | /* from 1-63 different squares posibles */ |
| 193 | |
| 194 | /*squares*/ |
| 195 | enum SQUARES { |
| 196 | A1,B1,C1,D1,E1,F1,G1,H1, |
| 197 | A2,B2,C2,D2,E2,F2,G2,H2, |
| 198 | A3,B3,C3,D3,E3,F3,G3,H3, |
| 199 | A4,B4,C4,D4,E4,F4,G4,H4, |
| 200 | A5,B5,C5,D5,E5,F5,G5,H5, |
| 201 | A6,B6,C6,D6,E6,F6,G6,H6, |
| 202 | A7,B7,C7,D7,E7,F7,G7,H7, |
| 203 | A8,B8,C8,D8,E8,F8,G8,H8, |
| 204 | NOSQUARE, |
| 205 | ERRSQUARE = 128 |
| 206 | }; |
| 207 | |
| 208 | /*------------------- end of inherited from a previous maindef.h -----------*/ |
| 209 | |
| 210 | #if !defined(NDEBUG) |
| 211 | #define NDEBUG |
| 212 | #endif |
| 213 | #ifdef DEBUG |
| 214 | #undef NDEBUG |
| 215 | #endif |
| 216 | #include "assert.h" |
| 217 | |
| 218 | /*------------------- general DEFINES--------------------------- -----------*/ |
| 219 | |
| 220 | #define gtbNOSIDE ((unsigned)-1) |
| 221 | #define gtbNOINDEX ((index_t)-1) |
| 222 | |
| 223 | /*************************************************\ |
| 224 | | |
| 225 | | COMPRESSION SCHEMES |
| 226 | | |
| 227 | \*************************************************/ |
| 228 | |
| 229 | #include "gtb-dec.h" |
| 230 | |
| 231 | static const char *const Extension[] = { |
| 232 | ".gtb.cp0" |
| 233 | ,".gtb.cp1" |
| 234 | ,".gtb.cp2" |
| 235 | ,".gtb.cp3" |
| 236 | ,".gtb.cp4" |
| 237 | ,".gtb.cp5" |
| 238 | ,".gtb.cp6" |
| 239 | ,".gtb.cp7" |
| 240 | ,".gtb.cp8" |
| 241 | ,".gtb.cp9" |
| 242 | }; |
| 243 | |
| 244 | /*************************************************\ |
| 245 | | |
| 246 | | MOVES |
| 247 | | |
| 248 | \*************************************************/ |
| 249 | |
| 250 | enum move_kind { |
| 251 | NORMAL_MOVE = 0, |
| 252 | CASTLE_MOVE, |
| 253 | PASSNT_MOVE, |
| 254 | PROMOT_MOVE |
| 255 | }; |
| 256 | |
| 257 | enum move_content { |
| 258 | NOMOVE = 0 |
| 259 | }; |
| 260 | |
| 261 | #define MV_TYPE(mv) ( (BYTE) ((mv) >>6 & 3 ) ) |
| 262 | #define MV_TO(mv) ( (SQUARE) ((mv) >>8 & 63) ) |
| 263 | #define MV_PT(mv) ( (SQ_CONTENT) ((mv) >>(3+16) &7 ) ) |
| 264 | #define MV_TK(mv) ( (SQ_CONTENT) ((mv) >>(6+16) &7 ) ) |
| 265 | #define MV_FROM(mv) ( (SQUARE) ((mv) & 63) ) |
| 266 | |
| 267 | /* |
| 268 | | move,type,color,piece,from,to,taken,promoted |
| 269 | *------------------------------------------------------------------*/ |
| 270 | |
| 271 | #define MV_BUILD(mv,ty,co,pc,fr,to,tk,pm) ( \ |
| 272 | (mv) = (fr) | (to)<< 8 | (ty)<< 6 | (co)<<8 \ |
| 273 | | (pc)<<16 | (pm)<< (3+16) | (tk)<< (6+16) \ |
| 274 | ) |
| 275 | |
| 276 | #define MV_ADD_TOTK(mv,to,tk) ( \ |
| 277 | mv |= (uint32_t)(to) << 8 \ |
| 278 | | (uint32_t)(tk) << (6+16) \ |
| 279 | ) |
| 280 | |
| 281 | #define map88(x) ( (x) + ((x)&070) ) |
| 282 | #define unmap88(x) ( ( (x) + ((x)& 07) ) >> 1 ) |
| 283 | |
| 284 | /*************************************************\ |
| 285 | | |
| 286 | | STATIC VARIABLES |
| 287 | | |
| 288 | \*************************************************/ |
| 289 | |
| 290 | static int GTB_scheme = 4; |
| 291 | |
| 292 | /*************************************************\ |
| 293 | | |
| 294 | | needed for |
| 295 | | PRE LOAD CACHE AND DEPENDENT FUNCTIONS |
| 296 | | |
| 297 | \*************************************************/ |
| 298 | |
| 299 | #define EGTB_MAXBLOCKSIZE 65536 |
| 300 | |
| 301 | static int GTB_MAXOPEN = 4; |
| 302 | |
| 303 | static bool_t Uncompressed = TRUE; |
| 304 | static unsigned char Buffer_zipped [EGTB_MAXBLOCKSIZE]; |
| 305 | static unsigned char Buffer_packed [EGTB_MAXBLOCKSIZE]; |
| 306 | static unsigned int zipinfo_init (void); |
| 307 | static void zipinfo_done (void); |
| 308 | |
| 309 | enum Flip_flags { |
| 310 | WE_FLAG = 1, NS_FLAG = 2, NW_SE_FLAG = 4 |
| 311 | }; /* used in flipt */ |
| 312 | |
| 313 | struct filesopen { |
| 314 | int n; |
| 315 | tbkey_t *key; |
| 316 | }; |
| 317 | |
| 318 | /* STATIC GLOBALS */ |
| 319 | |
| 320 | static struct filesopen fd = {0, NULL}; |
| 321 | |
| 322 | static bool_t TB_INITIALIZED = FALSE; |
| 323 | static bool_t DTM_CACHE_INITIALIZED = FALSE; |
| 324 | |
| 325 | static int WDL_FRACTION = 64; |
| 326 | static int WDL_FRACTION_MAX = 128; |
| 327 | |
| 328 | static size_t DTM_cache_size = 0; |
| 329 | static size_t WDL_cache_size = 0; |
| 330 | |
| 331 | static unsigned int TB_AVAILABILITY = 0; |
| 332 | |
| 333 | /* LOCKS */ |
| 334 | static mythread_mutex_t Egtb_lock; |
| 335 | |
| 336 | |
| 337 | /****************************************************************************\ |
| 338 | * |
| 339 | * |
| 340 | * DEBUGGING or PRINTING ZONE |
| 341 | * |
| 342 | * |
| 343 | ****************************************************************************/ |
| 344 | |
| 345 | #if 0 |
| 346 | #define FOLLOW_EGTB |
| 347 | #ifndef DEBUG |
| 348 | #define DEBUG |
| 349 | #endif |
| 350 | #endif |
| 351 | |
| 352 | #define validsq(x) ((x) >= A1 && (x) <= H8) |
| 353 | |
| 354 | #if defined(DEBUG) |
| 355 | static void print_pos (const sq_t *ws, const sq_t *bs, const pc_t *wp, const pc_t *bp); |
| 356 | #endif |
| 357 | |
| 358 | #if defined(DEBUG) || defined(FOLLOW_EGTB) |
| 359 | static void output_state (unsigned stm, const SQUARE *wSQ, const SQUARE *bSQ, |
| 360 | const SQ_CONTENT *wPC, const SQ_CONTENT *bPC); |
| 361 | static const char *Square_str[64] = { |
| 362 | "a1","b1","c1","d1","e1","f1","g1","h1", |
| 363 | "a2","b2","c2","d2","e2","f2","g2","h2", |
| 364 | "a3","b3","c3","d3","e3","f3","g3","h3", |
| 365 | "a4","b4","c4","d4","e4","f4","g4","h4", |
| 366 | "a5","b5","c5","d5","e5","f5","g5","h5", |
| 367 | "a6","b6","c6","d6","e6","f6","g6","h6", |
| 368 | "a7","b7","c7","d7","e7","f7","g7","h7", |
| 369 | "a8","b8","c8","d8","e8","f8","g8","h8" |
| 370 | }; |
| 371 | static const char *P_str[] = { |
| 372 | "--", "P", "N", "B", "R", "Q", "K" |
| 373 | }; |
| 374 | #endif |
| 375 | |
| 376 | #ifdef FOLLOW_EGTB |
| 377 | #define STAB |
| 378 | #define STABCONDITION 1 /*(stm == BL && whiteSQ[0]==H1 && whiteSQ[1]==D1 && whiteSQ[2]==D3 && blackSQ[0]==C2 )*/ |
| 379 | static bool_t GLOB_REPORT = TRUE; |
| 380 | #endif |
| 381 | |
| 382 | #if defined(FOLLOW_EGTB) |
| 383 | static const char *Info_str[8] = { |
| 384 | " Draw", " Wmate", " Bmate", "Illegal", |
| 385 | "~Draw", "~Wmate", "~Bmate", "Unknown" |
| 386 | }; |
| 387 | #endif |
| 388 | |
| 389 | static void list_index (void); |
| 390 | static void fatal_error(void) { |
| 391 | exit(EXIT_FAILURE); |
| 392 | } |
| 393 | |
| 394 | #ifdef STAB |
| 395 | #define FOLLOW_LU(x,y) {if (GLOB_REPORT) printf ("************** %s: %lu\n", (x), (long unsigned)(y));} |
| 396 | #else |
| 397 | #define FOLLOW_LU(x,y) |
| 398 | #endif |
| 399 | |
| 400 | #ifdef STAB |
| 401 | #define FOLLOW_LULU(x,y,z) {if (GLOB_REPORT) printf ("************** %s: %lu, %lu\n", (x), (long unsigned)(y), (long unsigned)(z));} |
| 402 | #else |
| 403 | #define FOLLOW_LULU(x,y,z) |
| 404 | #endif |
| 405 | |
| 406 | #ifdef STAB |
| 407 | #define FOLLOW_label(x) {if (GLOB_REPORT) printf ("************** %s\n", (x));} |
| 408 | #else |
| 409 | #define FOLLOW_label(x) |
| 410 | #endif |
| 411 | |
| 412 | #ifdef STAB |
| 413 | #define FOLLOW_DTM(msg,dtm) {if (GLOB_REPORT) printf ("************** %s: %lu, info:%s, plies:%lu \n"\ |
| 414 | , (msg), (long unsigned)(dtm), (Info_str[(dtm)&INFOMASK]), (long unsigned)((dtm)>>PLYSHIFT)\ |
| 415 | );} |
| 416 | #else |
| 417 | #define FOLLOW_DTM(msg,dtm) |
| 418 | #endif |
| 419 | |
| 420 | |
| 421 | /*--------------------------------*\ |
| 422 | | |
| 423 | | |
| 424 | | INDEXING FUNCTIONS |
| 425 | | |
| 426 | | |
| 427 | *---------------------------------*/ |
| 428 | |
| 429 | #define IDX_set_empty(x) {x=0;x--;} |
| 430 | #define IDX_is_empty(x) (0==(1+(x))) |
| 431 | |
| 432 | #define NO_KKINDEX NOINDEX |
| 433 | #define MAX_KKINDEX 462 |
| 434 | #define MAX_PPINDEX 576 |
| 435 | #define MAX_PpINDEX (24 * 48) |
| 436 | /*1128*/ |
| 437 | #define MAX_AAINDEX ((63-62) + (62 * (127-62)/2) - 1 + 1) |
| 438 | #define MAX_AAAINDEX (64*21*31) |
| 439 | #define MAX_PP48_INDEX (1128) |
| 440 | /* (24*23*22/6) + 24 * (24*23/2) */ |
| 441 | #define MAX_PPP48_INDEX 8648 |
| 442 | |
| 443 | /* VARIABLES */ |
| 444 | |
| 445 | static index_t kkidx [64] [64]; |
| 446 | static index_t ppidx [24] [48]; |
| 447 | static index_t pp48_idx[48][48]; |
| 448 | static index_t ppp48_idx[48][48][48]; |
| 449 | |
| 450 | static sq_t wksq [MAX_KKINDEX]; |
| 451 | static sq_t bksq [MAX_KKINDEX]; |
| 452 | static sq_t pp48_sq_x[MAX_PP48_INDEX]; |
| 453 | static sq_t pp48_sq_y[MAX_PP48_INDEX]; |
| 454 | |
| 455 | static index_t pp_hi24 [MAX_PPINDEX]; /* was unsigned int */ |
| 456 | static index_t pp_lo48 [MAX_PPINDEX]; |
| 457 | static unsigned int flipt [64] [64]; |
| 458 | static index_t aaidx [64] [64]; /* was unsigned int */ |
| 459 | static unsigned char aabase [MAX_AAINDEX]; |
| 460 | |
| 461 | static uint8_t ppp48_sq_x[MAX_PPP48_INDEX]; |
| 462 | static uint8_t ppp48_sq_y[MAX_PPP48_INDEX]; |
| 463 | static uint8_t ppp48_sq_z[MAX_PPP48_INDEX]; |
| 464 | |
| 465 | /* FUNCTIONS */ |
| 466 | |
| 467 | static void init_indexing (int verbosity); |
| 468 | static void norm_kkindex (SQUARE x, SQUARE y, /*@out@*/ SQUARE *pi, /*@out@*/ SQUARE *pj); |
| 469 | static void pp_putanchorfirst (SQUARE a, SQUARE b, /*@out@*/ SQUARE *out_anchor, /*@out@*/ SQUARE *out_loosen); |
| 470 | |
| 471 | static index_t wsq_to_pidx24 (SQUARE pawn); |
| 472 | static index_t wsq_to_pidx48 (SQUARE pawn); |
| 473 | static SQUARE pidx24_to_wsq (index_t a); |
| 474 | static SQUARE pidx48_to_wsq (index_t a); |
| 475 | |
| 476 | static SQUARE flipWE (SQUARE x) { return x ^ 07;} |
| 477 | static SQUARE flipNS (SQUARE x) { return x ^ 070;} |
| 478 | static SQUARE flipNW_SE (SQUARE x) { return ((x&7)<<3) | (x>>3);} |
| 479 | static SQUARE getcol (SQUARE x) { return x & 7;} |
| 480 | static SQUARE getrow (SQUARE x) { return x >> 3;} |
| 481 | static bool_t in_queenside (sq_t x) { return 0 == (x & (1<<2));} |
| 482 | |
| 483 | /* 1:0 */ |
| 484 | static void kxk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 485 | |
| 486 | /* 2:0 */ |
| 487 | static void kabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 488 | static void kakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 489 | static void kaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 490 | |
| 491 | /* 2:1 */ |
| 492 | static void kabkc_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 493 | static void kaakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 494 | |
| 495 | /* 3:0 */ |
| 496 | static void kabck_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 497 | static void kaabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 498 | static void kaaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 499 | static void kabbk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 500 | |
| 501 | /* one pawn */ |
| 502 | static void kpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 503 | |
| 504 | /* 1:1 one pawn */ |
| 505 | static void kakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 506 | |
| 507 | /* 2:0 one pawn */ |
| 508 | static void kapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 509 | |
| 510 | /* 2:0 two pawns */ |
| 511 | static void kppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 512 | |
| 513 | /* 2:1 one pawn */ |
| 514 | static void kapkb_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 515 | static void kabkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 516 | static void kaakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 517 | |
| 518 | /* 2:1 + 3:0 two pawns */ |
| 519 | static void kppka_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 520 | static void kappk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 521 | static void kapkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 522 | |
| 523 | /* 3:0 one pawn */ |
| 524 | static void kabpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 525 | static void kaapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 526 | |
| 527 | /* three pawns */ |
| 528 | static void kpppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 529 | static void kppkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 530 | |
| 531 | /* 1:1 two pawns */ |
| 532 | static void kpkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 533 | |
| 534 | /* corresponding pc to index */ |
| 535 | static bool_t kxk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 536 | static bool_t kabk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 537 | static bool_t kakb_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 538 | static bool_t kpk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 539 | static bool_t kakp_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 540 | static bool_t kapk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 541 | static bool_t kppk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 542 | static bool_t kaak_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 543 | static bool_t kabkc_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 544 | |
| 545 | static bool_t kaakb_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);/**/ |
| 546 | |
| 547 | static bool_t kabck_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 548 | static bool_t kaabk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);/**/ |
| 549 | static bool_t kaaak_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 550 | static bool_t kabbk_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out);/**/ |
| 551 | static bool_t kapkb_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 552 | static bool_t kabkp_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 553 | static bool_t kaakp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 554 | static bool_t kppka_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out); |
| 555 | static bool_t kappk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 556 | static bool_t kapkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 557 | static bool_t kabpk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 558 | static bool_t kaapk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 559 | static bool_t kppkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 560 | static bool_t kpppk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out); |
| 561 | static bool_t kpkp_pctoindex (const SQUARE *pw, const SQUARE *pb, /*@out@*/ index_t *out); |
| 562 | |
| 563 | /* testing functions */ |
| 564 | static bool_t test_kppk (void); |
| 565 | static bool_t test_kaakb (void); |
| 566 | static bool_t test_kaabk (void); |
| 567 | static bool_t test_kaaak (void); |
| 568 | static bool_t test_kabbk (void); |
| 569 | static bool_t test_kapkb (void); |
| 570 | static bool_t test_kabkp (void); |
| 571 | static bool_t test_kppka (void); |
| 572 | static bool_t test_kappk (void); |
| 573 | static bool_t test_kapkp (void); |
| 574 | static bool_t test_kabpk (void); |
| 575 | static bool_t test_kaapk (void); |
| 576 | static bool_t test_kaakp (void); |
| 577 | static bool_t test_kppkp (void); |
| 578 | static bool_t test_kpppk (void); |
| 579 | |
| 580 | static unsigned flip_type (SQUARE x, SQUARE y); |
| 581 | static index_t init_kkidx (void); |
| 582 | static index_t init_ppidx (void); |
| 583 | static void init_flipt (void); |
| 584 | static index_t init_aaidx (void); |
| 585 | static index_t init_aaa (void); |
| 586 | static index_t init_pp48_idx (void); |
| 587 | static index_t init_ppp48_idx (void); |
| 588 | |
| 589 | enum TB_INDEXES |
| 590 | { MAX_KXK = MAX_KKINDEX*64 |
| 591 | ,MAX_kabk = MAX_KKINDEX*64*64 |
| 592 | ,MAX_kakb = MAX_KKINDEX*64*64 |
| 593 | ,MAX_kpk = 24*64*64 |
| 594 | ,MAX_kakp = 24*64*64*64 |
| 595 | ,MAX_kapk = 24*64*64*64 |
| 596 | ,MAX_kppk = MAX_PPINDEX*64*64 |
| 597 | ,MAX_kpkp = MAX_PpINDEX*64*64 |
| 598 | ,MAX_kaak = MAX_KKINDEX*MAX_AAINDEX |
| 599 | ,MAX_kabkc = MAX_KKINDEX*64*64*64 |
| 600 | ,MAX_kabck = MAX_KKINDEX*64*64*64 |
| 601 | ,MAX_kaakb = MAX_KKINDEX*MAX_AAINDEX*64 |
| 602 | ,MAX_kaabk = MAX_KKINDEX*MAX_AAINDEX*64 |
| 603 | ,MAX_kabbk = MAX_KKINDEX*MAX_AAINDEX*64 |
| 604 | ,MAX_kaaak = MAX_KKINDEX*MAX_AAAINDEX |
| 605 | ,MAX_kapkb = 24*64*64*64*64 |
| 606 | ,MAX_kabkp = 24*64*64*64*64 |
| 607 | ,MAX_kabpk = 24*64*64*64*64 |
| 608 | ,MAX_kppka = MAX_kppk*64 |
| 609 | ,MAX_kappk = MAX_kppk*64 |
| 610 | ,MAX_kapkp = MAX_kpkp*64 |
| 611 | ,MAX_kaapk = 24*MAX_AAINDEX*64*64 |
| 612 | ,MAX_kaakp = 24*MAX_AAINDEX*64*64 |
| 613 | ,MAX_kppkp = 24*MAX_PP48_INDEX*64*64 |
| 614 | ,MAX_kpppk = MAX_PPP48_INDEX*64*64 |
| 615 | }; |
| 616 | |
| 617 | #if defined(SHARED_forbuilding) |
| 618 | extern index_t |
| 619 | biggest_memory_needed (void) { |
| 620 | return MAX_kabkc; |
| 621 | } |
| 622 | #endif |
| 623 | |
| 624 | /*--------------------------------*\ |
| 625 | | |
| 626 | | |
| 627 | | CACHE PROTOTYPES |
| 628 | | |
| 629 | | |
| 630 | *---------------------------------*/ |
| 631 | |
| 632 | #if !defined(SHARED_forbuilding) |
| 633 | mySHARED bool_t get_dtm (tbkey_t key, unsigned side, index_t idx, dtm_t *out, bool_t probe_hard); |
| 634 | #endif |
| 635 | |
| 636 | static bool_t get_dtm_from_cache (tbkey_t key, unsigned side, index_t idx, dtm_t *out); |
| 637 | |
| 638 | |
| 639 | /*--------------------------------*\ |
| 640 | | |
| 641 | | |
| 642 | | INIT |
| 643 | | |
| 644 | | |
| 645 | *---------------------------------*/ |
| 646 | |
| 647 | static bool_t fd_init (struct filesopen *pfd); |
| 648 | static void fd_done (struct filesopen *pfd); |
| 649 | |
| 650 | static void RAM_egtbfree (void); |
| 651 | |
| 652 | /*--------------------------------------------------------------------------*/ |
| 653 | #if !defined(SHARED_forbuilding) |
| 654 | mySHARED void egtb_freemem (int i); |
| 655 | #endif |
| 656 | |
| 657 | mySHARED struct endgamekey egkey[] = { |
| 658 | |
| 659 | {0, "kqk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 660 | {1, "krk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 661 | {2, "kbk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 662 | {3, "knk", MAX_KXK, 1, kxk_indextopc, kxk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 663 | {4, "kpk", MAX_kpk, 24,kpk_indextopc, kpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 664 | /* 4 pieces */ |
| 665 | {5, "kqkq", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 666 | {6, "kqkr", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 667 | {7, "kqkb", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 668 | {8, "kqkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 669 | |
| 670 | {9, "krkr", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 671 | {10,"krkb", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 672 | {11,"krkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 673 | |
| 674 | {12,"kbkb", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 675 | {13,"kbkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 676 | |
| 677 | {14,"knkn", MAX_kakb, 1, kakb_indextopc, kakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 678 | /**/ |
| 679 | {15,"kqqk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 680 | {16,"kqrk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 681 | {17,"kqbk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 682 | {18,"kqnk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 683 | |
| 684 | {19,"krrk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 685 | {20,"krbk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 686 | {21,"krnk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 687 | |
| 688 | {22,"kbbk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 689 | {23,"kbnk", MAX_kabk, 1, kabk_indextopc, kabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 690 | |
| 691 | {24,"knnk", MAX_kaak, 1, kaak_indextopc, kaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 692 | /**/ |
| 693 | /**/ |
| 694 | {25,"kqkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 695 | {26,"krkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 696 | {27,"kbkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 697 | {28,"knkp", MAX_kakp, 24,kakp_indextopc, kakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 698 | /**/ |
| 699 | {29,"kqpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 700 | {30,"krpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 701 | {31,"kbpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 702 | {32,"knpk", MAX_kapk, 24,kapk_indextopc, kapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 703 | /**/ |
| 704 | {33,"kppk", MAX_kppk, MAX_PPINDEX ,kppk_indextopc, kppk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 705 | /**/ |
| 706 | {34,"kpkp", MAX_kpkp, MAX_PpINDEX ,kpkp_indextopc, kpkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 707 | /**/ |
| 708 | /**/ |
| 709 | /* 5 pieces */ |
| 710 | { 35,"kqqqk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 711 | { 36,"kqqrk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 712 | { 37,"kqqbk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 713 | { 38,"kqqnk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 714 | { 39,"kqrrk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 715 | { 40,"kqrbk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 716 | { 41,"kqrnk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 717 | { 42,"kqbbk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 718 | { 43,"kqbnk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 719 | { 44,"kqnnk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 720 | { 45,"krrrk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 721 | { 46,"krrbk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 722 | { 47,"krrnk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 723 | { 48,"krbbk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 724 | { 49,"krbnk", MAX_kabck, 1, kabck_indextopc, kabck_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 725 | { 50,"krnnk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 726 | { 51,"kbbbk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 727 | { 52,"kbbnk", MAX_kaabk, 1, kaabk_indextopc, kaabk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 728 | { 53,"kbnnk", MAX_kabbk, 1, kabbk_indextopc, kabbk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 729 | { 54,"knnnk", MAX_kaaak, 1, kaaak_indextopc, kaaak_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 730 | { 55,"kqqkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 731 | { 56,"kqqkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 732 | { 57,"kqqkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 733 | { 58,"kqqkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 734 | { 59,"kqrkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 735 | { 60,"kqrkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 736 | { 61,"kqrkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 737 | { 62,"kqrkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 738 | { 63,"kqbkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 739 | { 64,"kqbkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 740 | { 65,"kqbkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 741 | { 66,"kqbkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 742 | { 67,"kqnkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 743 | { 68,"kqnkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 744 | { 69,"kqnkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 745 | { 70,"kqnkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 746 | { 71,"krrkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 747 | { 72,"krrkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 748 | { 73,"krrkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 749 | { 74,"krrkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 750 | { 75,"krbkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 751 | { 76,"krbkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 752 | { 77,"krbkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 753 | { 78,"krbkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 754 | { 79,"krnkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 755 | { 80,"krnkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 756 | { 81,"krnkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 757 | { 82,"krnkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 758 | { 83,"kbbkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 759 | { 84,"kbbkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 760 | { 85,"kbbkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 761 | { 86,"kbbkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 762 | { 87,"kbnkq", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 763 | { 88,"kbnkr", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 764 | { 89,"kbnkb", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 765 | { 90,"kbnkn", MAX_kabkc, 1, kabkc_indextopc, kabkc_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 766 | { 91,"knnkq", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 767 | { 92,"knnkr", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 768 | { 93,"knnkb", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 769 | { 94,"knnkn", MAX_kaakb, 1, kaakb_indextopc, kaakb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 770 | |
| 771 | { 95,"kqqpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 772 | { 96,"kqrpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 773 | { 97,"kqbpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 774 | { 98,"kqnpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 775 | { 99,"krrpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 776 | {100,"krbpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 777 | {101,"krnpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 778 | {102,"kbbpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 779 | {103,"kbnpk", MAX_kabpk, 24, kabpk_indextopc, kabpk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 780 | {104,"knnpk", MAX_kaapk, 24, kaapk_indextopc, kaapk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 781 | |
| 782 | {105,"kqppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 783 | {106,"krppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 784 | {107,"kbppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 785 | {108,"knppk", MAX_kappk, MAX_PPINDEX, kappk_indextopc, kappk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 786 | |
| 787 | {109,"kqpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 788 | {110,"kqpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 789 | {111,"kqpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 790 | {112,"kqpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 791 | {113,"krpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 792 | {114,"krpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 793 | {115,"krpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 794 | {116,"krpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 795 | {117,"kbpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 796 | {118,"kbpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 797 | {119,"kbpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 798 | {120,"kbpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 799 | {121,"knpkq", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 800 | {122,"knpkr", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 801 | {123,"knpkb", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 802 | {124,"knpkn", MAX_kapkb, 24, kapkb_indextopc, kapkb_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 803 | |
| 804 | {125,"kppkq", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 805 | {126,"kppkr", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 806 | {127,"kppkb", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 807 | {128,"kppkn", MAX_kppka, MAX_PPINDEX, kppka_indextopc, kppka_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 808 | |
| 809 | {129,"kqqkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 810 | {130,"kqrkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 811 | {131,"kqbkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 812 | {132,"kqnkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 813 | {133,"krrkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 814 | {134,"krbkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 815 | {135,"krnkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 816 | {136,"kbbkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 817 | {137,"kbnkp", MAX_kabkp, 24, kabkp_indextopc, kabkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 818 | {138,"knnkp", MAX_kaakp, 24, kaakp_indextopc, kaakp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 819 | |
| 820 | {139,"kqpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 821 | {140,"krpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 822 | {141,"kbpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 823 | {142,"knpkp", MAX_kapkp, MAX_PpINDEX, kapkp_indextopc, kapkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 824 | |
| 825 | {143,"kppkp", MAX_kppkp, 24*MAX_PP48_INDEX, kppkp_indextopc, kppkp_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 826 | {144,"kpppk", MAX_kpppk, MAX_PPP48_INDEX, kpppk_indextopc, kpppk_pctoindex, NULL , NULL ,NULL ,0, 0 }, |
| 827 | |
| 828 | {MAX_EGKEYS, NULL, 0, 1, NULL, NULL, NULL, NULL ,NULL ,0 ,0} |
| 829 | |
| 830 | }; |
| 831 | |
| 832 | static int eg_was_open[MAX_EGKEYS]; |
| 833 | |
| 834 | static uint64_t Bytes_read = 0; |
| 835 | |
| 836 | /****************************************************************************\ |
| 837 | | |
| 838 | | |
| 839 | | PATH MANAGEMENT ZONE |
| 840 | | |
| 841 | | |
| 842 | \****************************************************************************/ |
| 843 | |
| 844 | #define MAXPATHLEN tb_MAXPATHLEN |
| 845 | #define MAX_GTBPATHS 10 |
| 846 | |
| 847 | static int Gtbpath_end_index = 0; |
| 848 | static const char ** Gtbpath = NULL; |
| 849 | |
| 850 | /*---------------- EXTERNAL PATH MANAGEMENT --------------------------------*/ |
| 851 | |
| 852 | extern const char *tbpaths_getmain (void) { return Gtbpath[0];} |
| 853 | |
| 854 | extern const char ** |
| 855 | tbpaths_init(void) |
| 856 | { |
| 857 | const char **newps; |
| 858 | newps = (const char **) malloc (sizeof (char *)); |
| 859 | if (newps != NULL) { |
| 860 | newps[0] = NULL; |
| 861 | } |
| 862 | return newps; |
| 863 | } |
| 864 | |
| 865 | static const char ** |
| 866 | tbpaths_add_single(const char **ps, const char *newpath) |
| 867 | { |
| 868 | size_t counter; |
| 869 | const char **newps; |
| 870 | size_t i, psize; |
| 871 | char *ppath; |
| 872 | |
| 873 | if (NULL == ps) |
| 874 | return NULL; |
| 875 | |
| 876 | psize = strlen(newpath) + 1; |
| 877 | ppath = (char *) malloc (psize * sizeof (char)); |
| 878 | if (NULL == ppath) |
| 879 | return ps; /* failed to incorporate a new path */ |
| 880 | for (i = 0; i < psize; i++) ppath[i] = newpath[i]; |
| 881 | |
| 882 | for (counter = 0; ps[counter] != NULL; counter++) |
| 883 | ; |
| 884 | |
| 885 | /* cast to deal with const poisoning */ |
| 886 | newps = (const char **) realloc ((char **)ps, sizeof(char *) * (counter+2)); |
| 887 | if (newps != NULL) { |
| 888 | newps [counter] = ppath; |
| 889 | newps [counter+1] = NULL; |
| 890 | } |
| 891 | return newps; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | extern const char ** |
| 896 | tbpaths_add(const char **ps, const char *newpath) |
| 897 | { |
| 898 | size_t i, psize; |
| 899 | char *mpath; |
| 900 | |
| 901 | if (NULL == ps) |
| 902 | return NULL; |
| 903 | |
| 904 | psize = strlen(newpath) + 1; |
| 905 | mpath = (char *) malloc (psize * sizeof (char)); |
| 906 | if (NULL == mpath) { |
| 907 | return ps; /* failed to incorporate a new path */ |
| 908 | } |
| 909 | for (i = 0; i < psize; i++) mpath[i] = newpath[i]; |
| 910 | |
| 911 | for (i = 0; i < psize; i++) { |
| 912 | if(';' == mpath[i]) |
| 913 | mpath[i] = '\0'; |
| 914 | } |
| 915 | |
| 916 | for (i = 0;;) { |
| 917 | while (i < psize && mpath[i] == '\0') i++; |
| 918 | if (i >= psize) break; |
| 919 | ps = tbpaths_add_single (ps, &mpath[i]); |
| 920 | while (i < psize && mpath[i] != '\0') i++; |
| 921 | } |
| 922 | |
| 923 | free(mpath); |
| 924 | return ps; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | extern const char ** |
| 929 | tbpaths_done(const char **ps) |
| 930 | { |
| 931 | int counter; |
| 932 | void *q; |
| 933 | |
| 934 | if (ps != NULL) { |
| 935 | for (counter = 0; ps[counter] != NULL; counter++) { |
| 936 | /* cast to deal with const poisoning */ |
| 937 | void *p = (void *) ps[counter]; |
| 938 | free(p); |
| 939 | } |
| 940 | /* cast to deal with const poisoning */ |
| 941 | q = (void *) ps; |
| 942 | free(q); |
| 943 | } |
| 944 | return NULL; |
| 945 | } |
| 946 | |
| 947 | /*---------------- PATH INITIALIZATION ROUTINES ----------------------------*/ |
| 948 | |
| 949 | static void path_system_reset(void) {Gtbpath_end_index = 0;} |
| 950 | |
| 951 | static bool_t |
| 952 | path_system_init (const char **path) |
| 953 | { |
| 954 | size_t i; |
| 955 | size_t sz; |
| 956 | const char *x; |
| 957 | bool_t ok = TRUE; |
| 958 | path_system_reset(); |
| 959 | |
| 960 | if (path == NULL) { |
| 961 | return FALSE; |
| 962 | } |
| 963 | |
| 964 | /* calculate needed size for Gtbpath */ |
| 965 | i = 0; |
| 966 | do { |
| 967 | x = path[i++]; |
| 968 | } while (x != NULL); |
| 969 | sz = i; /* sz includes the NULL */ |
| 970 | |
| 971 | |
| 972 | Gtbpath = (const char **) malloc (sz * sizeof(char *)); |
| 973 | |
| 974 | if (Gtbpath) { |
| 975 | |
| 976 | ok = TRUE; |
| 977 | /* point to the same strings provided */ |
| 978 | Gtbpath_end_index = 0; |
| 979 | for (i = 0; i < sz; i++) { |
| 980 | Gtbpath[i] = path[i]; |
| 981 | Gtbpath_end_index++; |
| 982 | } |
| 983 | |
| 984 | } else { |
| 985 | ok = FALSE; |
| 986 | } |
| 987 | return ok; |
| 988 | |
| 989 | } |
| 990 | |
| 991 | static void |
| 992 | path_system_done (void) |
| 993 | { |
| 994 | /* before we free Gtbpath, we have to deal with the |
| 995 | "const poisoning" and cast it. free() does not accept |
| 996 | const pointers */ |
| 997 | char ** p = (char **) Gtbpath; |
| 998 | /* clean up */ |
| 999 | if (p != NULL) |
| 1000 | free(p); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | /****************************************************************************\ |
| 1006 | * |
| 1007 | * |
| 1008 | * General Initialization Zone |
| 1009 | * |
| 1010 | * |
| 1011 | ****************************************************************************/ |
| 1012 | |
| 1013 | |
| 1014 | #ifdef WDL_PROBE |
| 1015 | static size_t wdl_cache_init (size_t cache_mem); |
| 1016 | static void wdl_cache_flush (void); |
| 1017 | |
| 1018 | static void wdl_cache_reset_counters (void); |
| 1019 | static void wdl_cache_done (void); |
| 1020 | |
| 1021 | static bool_t get_WDL_from_cache (tbkey_t key, unsigned side, index_t idx, unsigned int *out); |
| 1022 | static bool_t wdl_preload_cache (tbkey_t key, unsigned side, index_t idx); |
| 1023 | #endif |
| 1024 | |
| 1025 | #ifdef GTB_SHARE |
| 1026 | static void init_bettarr (void); |
| 1027 | #endif |
| 1028 | |
| 1029 | static void eg_was_open_reset(void) |
| 1030 | { |
| 1031 | int i; |
| 1032 | for (i = 0; i < MAX_EGKEYS; i++) { |
| 1033 | eg_was_open[i] = 0; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | static long unsigned int eg_was_open_count(void) |
| 1038 | { |
| 1039 | long int i, x; |
| 1040 | for (i = 0, x = 0; i < MAX_EGKEYS; i++) { |
| 1041 | x += eg_was_open[i]; |
| 1042 | } |
| 1043 | return (long unsigned) x; |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | enum Sizes {INISIZE = 4096}; |
| 1048 | static char ini_str[INISIZE]; |
| 1049 | static void sjoin(char *s, const char *tail, size_t max) {strncat(s, tail, max - strlen(s) - 1);} |
| 1050 | |
| 1051 | char * |
| 1052 | tb_init (int verbosity, int decoding_sch, const char **paths) |
| 1053 | { |
| 1054 | unsigned int zi; |
| 1055 | int paths_ok; |
| 1056 | char *ret_str; |
| 1057 | char localstr[256]; |
| 1058 | |
| 1059 | assert(!TB_INITIALIZED); |
| 1060 | |
| 1061 | if (verbosity) { |
| 1062 | ini_str[0] = '\0'; |
| 1063 | ret_str = ini_str; |
| 1064 | } else { |
| 1065 | ret_str = NULL; |
| 1066 | } |
| 1067 | |
| 1068 | paths_ok = path_system_init (paths); |
| 1069 | |
| 1070 | if (paths_ok && verbosity) { |
| 1071 | int g; |
| 1072 | assert(Gtbpath!=NULL); |
| 1073 | sjoin(ini_str,"\nGTB PATHS\n",INISIZE); |
| 1074 | for (g = 0; Gtbpath[g] != NULL; g++) { |
| 1075 | const char *p = Gtbpath[g]; |
| 1076 | if (0 == g) { |
| 1077 | sprintf (localstr," main: %s\n", p); |
| 1078 | } else { |
| 1079 | sprintf (localstr," #%d: %s\n", g, p); |
| 1080 | } |
| 1081 | sjoin(ini_str,localstr,INISIZE); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | if (!paths_ok && verbosity) { |
| 1086 | sjoin (ini_str,"\nGTB PATHS not initialized\n",INISIZE); |
| 1087 | } |
| 1088 | |
| 1089 | if (!reach_was_initialized()) |
| 1090 | reach_init(); |
| 1091 | |
| 1092 | attack_maps_init (); /* external initialization */ |
| 1093 | |
| 1094 | init_indexing(0 /* no verbosity */); |
| 1095 | |
| 1096 | #ifdef GTB_SHARE |
| 1097 | init_bettarr(); |
| 1098 | #endif |
| 1099 | |
| 1100 | if (!fd_init (&fd) && verbosity) { |
| 1101 | sjoin (ini_str," File Open Memory initialization = **FAILED**\n",INISIZE); |
| 1102 | return ret_str; |
| 1103 | } |
| 1104 | |
| 1105 | GTB_scheme = decoding_sch; |
| 1106 | Uncompressed = GTB_scheme == 0; |
| 1107 | |
| 1108 | if (GTB_scheme == 0) { |
| 1109 | Uncompressed = TRUE; |
| 1110 | } |
| 1111 | |
| 1112 | set_decoding_scheme(GTB_scheme); |
| 1113 | |
| 1114 | if (verbosity) { |
| 1115 | sjoin (ini_str,"\nGTB initialization\n",INISIZE); |
| 1116 | sprintf (localstr," Compression Scheme = %d\n", GTB_scheme); |
| 1117 | sjoin (ini_str,localstr,INISIZE); |
| 1118 | } |
| 1119 | |
| 1120 | zi = zipinfo_init(); |
| 1121 | |
| 1122 | TB_AVAILABILITY = zi; |
| 1123 | |
| 1124 | if (verbosity) { |
| 1125 | if (0 == zi) { |
| 1126 | sjoin (ini_str," Compression Indexes = **FAILED**\n",INISIZE); |
| 1127 | } else { |
| 1128 | int n, bit; |
| 1129 | |
| 1130 | n = 3; bit = 1; |
| 1131 | if (zi&(1u<<bit)) |
| 1132 | sprintf (localstr," Compression Indexes (%d-pc) = PASSED\n",n); |
| 1133 | else |
| 1134 | sprintf (localstr," Compression Indexes (%d-pc) = **FAILED**\n",n); |
| 1135 | sjoin (ini_str,localstr,INISIZE); |
| 1136 | |
| 1137 | n = 4; bit = 3; |
| 1138 | if (zi&(1u<<bit)) |
| 1139 | sprintf (localstr," Compression Indexes (%d-pc) = PASSED\n",n); |
| 1140 | else |
| 1141 | sprintf (localstr," Compression Indexes (%d-pc) = **FAILED**\n",n); |
| 1142 | sjoin (ini_str,localstr,INISIZE); |
| 1143 | |
| 1144 | n = 5; bit = 5; |
| 1145 | if (zi&(1u<<bit)) |
| 1146 | sprintf (localstr," Compression Indexes (%d-pc) = PASSED\n",n); |
| 1147 | else |
| 1148 | sprintf (localstr," Compression Indexes (%d-pc) = **FAILED**\n",n); |
| 1149 | sjoin (ini_str,localstr,INISIZE); |
| 1150 | } |
| 1151 | sjoin (ini_str,"\n",INISIZE); |
| 1152 | } |
| 1153 | |
| 1154 | eg_was_open_reset(); |
| 1155 | Bytes_read = 0; |
| 1156 | |
| 1157 | mythread_mutex_init (&Egtb_lock); |
| 1158 | |
| 1159 | TB_INITIALIZED = TRUE; |
| 1160 | |
| 1161 | return ret_str; |
| 1162 | } |
| 1163 | |
| 1164 | extern unsigned int |
| 1165 | tb_availability(void) |
| 1166 | { |
| 1167 | return TB_AVAILABILITY; |
| 1168 | } |
| 1169 | |
| 1170 | extern bool_t |
| 1171 | tb_is_initialized (void) |
| 1172 | { |
| 1173 | return TB_INITIALIZED; |
| 1174 | } |
| 1175 | |
| 1176 | extern void |
| 1177 | tb_done (void) |
| 1178 | { |
| 1179 | assert(TB_INITIALIZED); |
| 1180 | fd_done (&fd); |
| 1181 | RAM_egtbfree(); |
| 1182 | zipinfo_done(); |
| 1183 | path_system_done(); |
| 1184 | mythread_mutex_destroy (&Egtb_lock); |
| 1185 | TB_INITIALIZED = FALSE; |
| 1186 | |
| 1187 | /* |
| 1188 | HERE, I should be free() the ini_str, but in |
| 1189 | the current implementation, it is static |
| 1190 | rather than dynamic. |
| 1191 | */ |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | char * |
| 1196 | tb_restart(int verbosity, int decoding_sch, const char **paths) |
| 1197 | { |
| 1198 | if (tb_is_initialized()) { |
| 1199 | tb_done(); |
| 1200 | } |
| 1201 | return tb_init(verbosity, decoding_sch, paths); |
| 1202 | } |
| 1203 | |
| 1204 | /* whenever the program exits should release this memory */ |
| 1205 | static void |
| 1206 | RAM_egtbfree (void) |
| 1207 | { |
| 1208 | int i; |
| 1209 | for (i = 0; egkey[i].str != NULL; i++) { |
| 1210 | egtb_freemem (i); |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | /*--------------------------------------------------------------------------*/ |
| 1215 | |
| 1216 | #ifdef GTB_SHARE |
| 1217 | static void |
| 1218 | init_bettarr (void) |
| 1219 | { |
| 1220 | /* |
| 1221 | iDRAW = 0, iWMATE = 1, iBMATE = 2, iFORBID = 3, |
| 1222 | iDRAWt = 4, iWMATEt = 5, iBMATEt = 6, iUNKNOWN = 7 |
| 1223 | */ |
| 1224 | |
| 1225 | int temp[] = { |
| 1226 | /*White*/ |
| 1227 | /*iDRAW vs*/ |
| 1228 | 1, 2, 1, 1, 2, 2, 2, 2, |
| 1229 | /*iWMATE vs*/ |
| 1230 | 1, 3, 1, 1, 1, 1, 1, 1, |
| 1231 | /*iBMATE vs*/ |
| 1232 | 2, 2, 4, 1, 2, 2, 2, 2, |
| 1233 | /*iFORBID vs*/ |
| 1234 | 2, 2, 2, 2, 2, 2, 2, 2, |
| 1235 | |
| 1236 | /*iDRAWt vs*/ |
| 1237 | 1, 2, 1, 1, 2, 2, 1, 2, |
| 1238 | /*iWMATEt vs*/ |
| 1239 | 1, 2, 1, 1, 1, 3, 1, 1, |
| 1240 | /*iBMATEt vs*/ |
| 1241 | 1, 2, 1, 1, 2, 2, 4, 2, |
| 1242 | /*iUNKNOWN */ |
| 1243 | 1, 2, 1, 1, 1, 2, 1, 2, |
| 1244 | |
| 1245 | /*Black*/ |
| 1246 | /*iDRAW vs*/ |
| 1247 | 1, 1, 2, 1, 2, 2, 2, 2, |
| 1248 | /*iWMATE vs*/ |
| 1249 | 2, 4, 2, 1, 2, 2, 2, 2, |
| 1250 | /*iBMATE vs*/ |
| 1251 | 1, 1, 3, 1, 1, 1, 1, 1, |
| 1252 | /*iFORBID vs*/ |
| 1253 | 2, 2, 2, 2, 2, 2, 2, 2, |
| 1254 | |
| 1255 | /*iDRAWt vs*/ |
| 1256 | 1, 1, 2, 1, 2, 1, 2, 2, |
| 1257 | /*iWMATEt vs*/ |
| 1258 | 1, 1, 2, 1, 2, 4, 2, 2, |
| 1259 | /*iBMATEt vs*/ |
| 1260 | 1, 1, 2, 1, 1, 1, 3, 1, |
| 1261 | /*iUNKNOWN */ |
| 1262 | 1, 1, 2, 1, 1, 1, 2, 2 |
| 1263 | }; |
| 1264 | |
| 1265 | int i, j, k, z; |
| 1266 | |
| 1267 | /* reset */ |
| 1268 | z = 0; |
| 1269 | for (i = 0; i < 2; i++) |
| 1270 | for (j = 0; j < 8; j++) |
| 1271 | for (k = 0; k < 8; k++) |
| 1272 | bettarr [i][j][k] = temp[z++]; |
| 1273 | |
| 1274 | return; |
| 1275 | } |
| 1276 | #endif |
| 1277 | |
| 1278 | /* |
| 1279 | | |
| 1280 | | Own File Descriptors |
| 1281 | | |
| 1282 | \*---------------------------------------------------------------------------*/ |
| 1283 | |
| 1284 | static bool_t |
| 1285 | fd_init (struct filesopen *pfd) |
| 1286 | { |
| 1287 | tbkey_t *p; |
| 1288 | int i, allowed; |
| 1289 | |
| 1290 | pfd->n = 0; |
| 1291 | |
| 1292 | allowed = mysys_fopen_max() - 5 /*stdin,stdout,sterr,stdlog,book*/; |
| 1293 | if (allowed < 4) |
| 1294 | GTB_MAXOPEN = 4; |
| 1295 | if (allowed > 32) |
| 1296 | GTB_MAXOPEN = 32; |
| 1297 | |
| 1298 | p = (tbkey_t *) malloc(sizeof(tbkey_t)*(size_t)GTB_MAXOPEN); |
| 1299 | |
| 1300 | if (p != NULL) { |
| 1301 | for (i = 0; i < GTB_MAXOPEN; i++) { |
| 1302 | p[i] = -1; |
| 1303 | } |
| 1304 | pfd->key = p; |
| 1305 | return TRUE; |
| 1306 | } else { |
| 1307 | return FALSE; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | static void |
| 1312 | fd_done (struct filesopen *pfd) |
| 1313 | { |
| 1314 | int i; |
| 1315 | tbkey_t closingkey; |
| 1316 | FILE *finp; |
| 1317 | |
| 1318 | assert(pfd != NULL); |
| 1319 | |
| 1320 | for (i = 0; i < pfd->n; i++) { |
| 1321 | closingkey = pfd->key[i]; |
| 1322 | finp = egkey [closingkey].fd; |
| 1323 | fclose (finp); |
| 1324 | egkey[closingkey].fd = NULL; |
| 1325 | pfd->key[i] = -1; |
| 1326 | } |
| 1327 | pfd->n = 0; |
| 1328 | free(pfd->key); |
| 1329 | } |
| 1330 | |
| 1331 | /****************************************************************************\ |
| 1332 | | |
| 1333 | | |
| 1334 | | PROBE ZONE |
| 1335 | | |
| 1336 | | |
| 1337 | \****************************************************************************/ |
| 1338 | |
| 1339 | #if !defined(SHARED_forbuilding) |
| 1340 | |
| 1341 | /* shared with building routines */ |
| 1342 | mySHARED void list_sq_copy (const SQUARE *a, SQUARE *b); |
| 1343 | mySHARED void list_pc_copy (const SQ_CONTENT *a, SQ_CONTENT *b); |
| 1344 | mySHARED dtm_t inv_dtm (dtm_t x); |
| 1345 | mySHARED bool_t egtb_get_id (SQ_CONTENT *w, SQ_CONTENT *b, tbkey_t *id); |
| 1346 | mySHARED void list_sq_flipNS (SQUARE *s); |
| 1347 | mySHARED dtm_t adjust_up (dtm_t dist); |
| 1348 | mySHARED dtm_t bestx (unsigned stm, dtm_t a, dtm_t b); |
| 1349 | mySHARED void sortlists (SQUARE *ws, SQ_CONTENT *wp); |
| 1350 | |
| 1351 | mySHARED /*@NULL@*/ FILE * fd_openit(tbkey_t key); |
| 1352 | |
| 1353 | mySHARED dtm_t dtm_unpack (unsigned stm, unsigned char packed); |
| 1354 | mySHARED void unpackdist (dtm_t d, unsigned int *res, unsigned int *ply); |
| 1355 | mySHARED dtm_t packdist (unsigned int inf, unsigned int ply); |
| 1356 | |
| 1357 | mySHARED bool_t fread_entry_packed (FILE *dest, unsigned side, dtm_t *px); |
| 1358 | mySHARED bool_t fpark_entry_packed (FILE *finp, unsigned side, index_t max, index_t idx); |
| 1359 | #endif |
| 1360 | |
| 1361 | /* use only with probe */ |
| 1362 | static bool_t egtb_get_dtm (tbkey_t k, unsigned stm, const SQUARE *wS, const SQUARE *bS, bool_t probe_hard, dtm_t *dtm); |
| 1363 | static void removepiece (SQUARE *ys, SQ_CONTENT *yp, int j); |
| 1364 | static bool_t egtb_filepeek (tbkey_t key, unsigned side, index_t idx, dtm_t *out_dtm); |
| 1365 | |
| 1366 | |
| 1367 | /*prototype*/ |
| 1368 | #ifdef WDL_PROBE |
| 1369 | static bool_t |
| 1370 | tb_probe_wdl |
| 1371 | (unsigned stm, |
| 1372 | const SQUARE *inp_wSQ, |
| 1373 | const SQUARE *inp_bSQ, |
| 1374 | const SQ_CONTENT *inp_wPC, |
| 1375 | const SQ_CONTENT *inp_bPC, |
| 1376 | bool_t probingtype, |
| 1377 | /*@out@*/ unsigned *res); |
| 1378 | #endif |
| 1379 | |
| 1380 | static bool_t |
| 1381 | tb_probe_ (unsigned stm, |
| 1382 | SQUARE epsq, |
| 1383 | const SQUARE *inp_wSQ, |
| 1384 | const SQUARE *inp_bSQ, |
| 1385 | const SQ_CONTENT *inp_wPC, |
| 1386 | const SQ_CONTENT *inp_bPC, |
| 1387 | bool_t probingtype, |
| 1388 | /*@out@*/ unsigned *res, |
| 1389 | /*@out@*/ unsigned *ply); |
| 1390 | |
| 1391 | |
| 1392 | extern bool_t |
| 1393 | tb_probe_soft |
| 1394 | (unsigned stm, |
| 1395 | SQUARE epsq, |
| 1396 | unsigned castles, |
| 1397 | const SQUARE *inp_wSQ, |
| 1398 | const SQUARE *inp_bSQ, |
| 1399 | const SQ_CONTENT *inp_wPC, |
| 1400 | const SQ_CONTENT *inp_bPC, |
| 1401 | /*@out@*/ unsigned *res, |
| 1402 | /*@out@*/ unsigned *ply) |
| 1403 | { |
| 1404 | if (castles != 0) |
| 1405 | return FALSE; |
| 1406 | return tb_probe_ (stm, epsq, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, FALSE, res, ply); |
| 1407 | } |
| 1408 | |
| 1409 | extern bool_t |
| 1410 | tb_probe_hard |
| 1411 | (unsigned stm, |
| 1412 | SQUARE epsq, |
| 1413 | unsigned castles, |
| 1414 | const SQUARE *inp_wSQ, |
| 1415 | const SQUARE *inp_bSQ, |
| 1416 | const SQ_CONTENT *inp_wPC, |
| 1417 | const SQ_CONTENT *inp_bPC, |
| 1418 | /*@out@*/ unsigned *res, |
| 1419 | /*@out@*/ unsigned *ply) |
| 1420 | { |
| 1421 | if (castles != 0) |
| 1422 | return FALSE; |
| 1423 | return tb_probe_ (stm, epsq, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, TRUE, res, ply); |
| 1424 | } |
| 1425 | |
| 1426 | extern bool_t |
| 1427 | tb_probe_WDL_soft |
| 1428 | (unsigned stm, |
| 1429 | SQUARE epsq, |
| 1430 | unsigned castles, |
| 1431 | const SQUARE *inp_wSQ, |
| 1432 | const SQUARE *inp_bSQ, |
| 1433 | const SQ_CONTENT *inp_wPC, |
| 1434 | const SQ_CONTENT *inp_bPC, |
| 1435 | /*@out@*/ unsigned *res) |
| 1436 | { |
| 1437 | unsigned ply_n; |
| 1438 | unsigned *ply = &ply_n; |
| 1439 | if (castles != 0) |
| 1440 | return FALSE; |
| 1441 | if (epsq != NOSQUARE) |
| 1442 | return tb_probe_ (stm, epsq, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, FALSE, res, ply); |
| 1443 | |
| 1444 | /* probe bitbase like, assuming no en passant */ |
| 1445 | #ifdef WDL_PROBE |
| 1446 | return tb_probe_wdl (stm, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, FALSE, res); |
| 1447 | #else |
| 1448 | return tb_probe_ (stm, epsq, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, FALSE, res, ply); |
| 1449 | #endif |
| 1450 | } |
| 1451 | |
| 1452 | extern bool_t |
| 1453 | tb_probe_WDL_hard |
| 1454 | (unsigned stm, |
| 1455 | SQUARE epsq, |
| 1456 | unsigned castles, |
| 1457 | const SQUARE *inp_wSQ, |
| 1458 | const SQUARE *inp_bSQ, |
| 1459 | const SQ_CONTENT *inp_wPC, |
| 1460 | const SQ_CONTENT *inp_bPC, |
| 1461 | /*@out@*/ unsigned *res) |
| 1462 | { |
| 1463 | unsigned ply_n; |
| 1464 | unsigned *ply = &ply_n; |
| 1465 | if (castles != 0) |
| 1466 | return FALSE; |
| 1467 | if (epsq != NOSQUARE) |
| 1468 | return tb_probe_ (stm, epsq, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, TRUE, res, ply); |
| 1469 | |
| 1470 | /* probe bitbase like, assuming no en passant */ |
| 1471 | #ifdef WDL_PROBE |
| 1472 | return tb_probe_wdl (stm, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, TRUE, res); |
| 1473 | #else |
| 1474 | return tb_probe_ (stm, epsq, inp_wSQ, inp_bSQ, inp_wPC, inp_bPC, TRUE, res, ply); |
| 1475 | #endif |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | static bool_t |
| 1480 | tb_probe_ (unsigned stm, |
| 1481 | SQUARE epsq, |
| 1482 | const SQUARE *inp_wSQ, |
| 1483 | const SQUARE *inp_bSQ, |
| 1484 | const SQ_CONTENT *inp_wPC, |
| 1485 | const SQ_CONTENT *inp_bPC, |
| 1486 | bool_t probingtype, |
| 1487 | /*@out@*/ unsigned *res, |
| 1488 | /*@out@*/ unsigned *ply) |
| 1489 | { |
| 1490 | int i = 0, j = 0; |
| 1491 | tbkey_t id = -1; |
| 1492 | dtm_t dtm; |
| 1493 | |
| 1494 | SQUARE storage_ws [MAX_LISTSIZE], storage_bs [MAX_LISTSIZE]; |
| 1495 | SQ_CONTENT storage_wp [MAX_LISTSIZE], storage_bp [MAX_LISTSIZE]; |
| 1496 | |
| 1497 | SQUARE *ws = storage_ws; |
| 1498 | SQUARE *bs = storage_bs; |
| 1499 | SQ_CONTENT *wp = storage_wp; |
| 1500 | SQ_CONTENT *bp = storage_bp; |
| 1501 | |
| 1502 | SQUARE *xs; |
| 1503 | SQUARE *ys; |
| 1504 | SQ_CONTENT *xp; |
| 1505 | SQ_CONTENT *yp; |
| 1506 | |
| 1507 | SQUARE tmp_ws [MAX_LISTSIZE], tmp_bs [MAX_LISTSIZE]; |
| 1508 | SQ_CONTENT tmp_wp [MAX_LISTSIZE], tmp_bp [MAX_LISTSIZE]; |
| 1509 | |
| 1510 | SQUARE *temps; |
| 1511 | bool_t straight = FALSE; |
| 1512 | |
| 1513 | SQUARE capturer_a, capturer_b, xed = NOSQUARE; |
| 1514 | |
| 1515 | unsigned int plies; |
| 1516 | unsigned int inf; |
| 1517 | |
| 1518 | bool_t okdtm = TRUE; |
| 1519 | bool_t okcall = TRUE; |
| 1520 | |
| 1521 | /************************************/ |
| 1522 | |
| 1523 | assert (stm == WH || stm == BL); |
| 1524 | /*assert (inp_wPC[0] == KING && inp_bPC[0] == KING );*/ |
| 1525 | assert ((epsq >> 3) == 2 || (epsq >> 3) == 5 || epsq == NOSQUARE); |
| 1526 | |
| 1527 | /* VALID ONLY FOR KK!! */ |
| 1528 | if (inp_wPC[1] == NOPIECE && inp_bPC[1] == NOPIECE) { |
| 1529 | index_t dummy_i; |
| 1530 | bool_t b = kxk_pctoindex (inp_wSQ, inp_bSQ, &dummy_i); |
| 1531 | *res = b? iDRAW: iFORBID; |
| 1532 | *ply = 0; |
| 1533 | return TRUE; |
| 1534 | } |
| 1535 | |
| 1536 | /* copy input */ |
| 1537 | list_pc_copy (inp_wPC, wp); |
| 1538 | list_pc_copy (inp_bPC, bp); |
| 1539 | list_sq_copy (inp_wSQ, ws); |
| 1540 | list_sq_copy (inp_bSQ, bs); |
| 1541 | |
| 1542 | sortlists (ws, wp); |
| 1543 | sortlists (bs, bp); |
| 1544 | |
| 1545 | FOLLOW_label("EGTB_PROBE") |
| 1546 | |
| 1547 | if (egtb_get_id (wp, bp, &id)) { |
| 1548 | FOLLOW_LU("got ID",id) |
| 1549 | straight = TRUE; |
| 1550 | } else if (egtb_get_id (bp, wp, &id)) { |
| 1551 | FOLLOW_LU("rev ID",id) |
| 1552 | straight = FALSE; |
| 1553 | list_sq_flipNS (ws); |
| 1554 | list_sq_flipNS (bs); |
| 1555 | temps = ws; |
| 1556 | ws = bs; |
| 1557 | bs = temps; |
| 1558 | stm = Opp(stm); |
| 1559 | if (epsq != NOSQUARE) epsq ^= 070; /* added */ |
| 1560 | {SQ_CONTENT *tempp = wp; wp = bp; bp = tempp;} /* added */ |
| 1561 | } else { |
| 1562 | #if defined(DEBUG) |
| 1563 | printf("did not get id...\n"); |
| 1564 | output_state (stm, ws, bs, wp, bp); |
| 1565 | #endif |
| 1566 | unpackdist (iFORBID, res, ply); |
| 1567 | return FALSE; |
| 1568 | } |
| 1569 | |
| 1570 | /* store position... */ |
| 1571 | list_pc_copy (wp, tmp_wp); |
| 1572 | list_pc_copy (bp, tmp_bp); |
| 1573 | list_sq_copy (ws, tmp_ws); |
| 1574 | list_sq_copy (bs, tmp_bs); |
| 1575 | |
| 1576 | /* x will be stm and y will be stw */ |
| 1577 | if (stm == WH) { |
| 1578 | xs = ws; |
| 1579 | xp = wp; |
| 1580 | ys = bs; |
| 1581 | yp = bp; |
| 1582 | } else { |
| 1583 | xs = bs; |
| 1584 | xp = bp; |
| 1585 | ys = ws; |
| 1586 | yp = wp; |
| 1587 | } |
| 1588 | |
| 1589 | okdtm = egtb_get_dtm (id, stm, ws, bs, probingtype, &dtm); |
| 1590 | |
| 1591 | FOLLOW_LU("dtmok?",okdtm) |
| 1592 | FOLLOW_DTM("dtm", dtm) |
| 1593 | |
| 1594 | if (okdtm) { |
| 1595 | |
| 1596 | capturer_a = NOSQUARE; |
| 1597 | capturer_b = NOSQUARE; |
| 1598 | |
| 1599 | if (epsq != NOSQUARE) { |
| 1600 | /* captured pawn, trick: from epsquare to captured */ |
| 1601 | xed = epsq ^ (1<<3); |
| 1602 | |
| 1603 | /* find index captured (j) */ |
| 1604 | for (j = 0; ys[j] != NOSQUARE; j++) { |
| 1605 | if (ys[j] == xed) break; |
| 1606 | } |
| 1607 | |
| 1608 | /* try first possible ep capture */ |
| 1609 | if (0 == (0x88 & (map88(xed) + 1))) |
| 1610 | capturer_a = xed + 1; |
| 1611 | /* try second possible ep capture */ |
| 1612 | if (0 == (0x88 & (map88(xed) - 1))) |
| 1613 | capturer_b = xed - 1; |
| 1614 | |
| 1615 | if (ys[j] == xed) { |
| 1616 | |
| 1617 | /* find capturers (i) */ |
| 1618 | for (i = 0; xs[i] != NOSQUARE && okcall; i++) { |
| 1619 | |
| 1620 | if (xp[i]==PAWN && (xs[i]==capturer_a || xs[i]==capturer_b)) { |
| 1621 | dtm_t epscore = iFORBID; |
| 1622 | |
| 1623 | /* execute capture */ |
| 1624 | xs[i] = epsq; |
| 1625 | removepiece (ys, yp, j); |
| 1626 | |
| 1627 | okcall = tb_probe_ (Opp(stm), NOSQUARE, ws, bs, wp, bp, probingtype, &inf, &plies); |
| 1628 | |
| 1629 | if (okcall) { |
| 1630 | epscore = packdist (inf, plies); |
| 1631 | epscore = adjust_up (epscore); |
| 1632 | |
| 1633 | /* chooses to ep or not */ |
| 1634 | dtm = bestx (stm, epscore, dtm); |
| 1635 | } |
| 1636 | |
| 1637 | /* restore position */ |
| 1638 | list_pc_copy (tmp_wp, wp); |
| 1639 | list_pc_copy (tmp_bp, bp); |
| 1640 | |
| 1641 | list_sq_copy (tmp_ws, ws); |
| 1642 | list_sq_copy (tmp_bs, bs); |
| 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | } /* ep */ |
| 1647 | |
| 1648 | if (straight) { |
| 1649 | unpackdist (dtm, res, ply); |
| 1650 | } else { |
| 1651 | unpackdist (inv_dtm (dtm), res, ply); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | if (!okdtm || !okcall) { |
| 1656 | unpackdist (iFORBID, res, ply); |
| 1657 | } |
| 1658 | |
| 1659 | return okdtm && okcall; |
| 1660 | } |
| 1661 | |
| 1662 | #ifdef _MSC_VER |
| 1663 | /* to silence warning for sprintf usage */ |
| 1664 | #pragma warning(disable:4996) |
| 1665 | #endif |
| 1666 | |
| 1667 | static bool_t |
| 1668 | egtb_filepeek (tbkey_t key, unsigned side, index_t idx, dtm_t *out_dtm) |
| 1669 | { |
| 1670 | FILE *finp; |
| 1671 | |
| 1672 | #define USE_FD |
| 1673 | |
| 1674 | #if !defined(USE_FD) |
| 1675 | char buf[1024]; |
| 1676 | char *filename = buf; |
| 1677 | #endif |
| 1678 | |
| 1679 | bool_t ok; |
| 1680 | dtm_t x=0; |
| 1681 | index_t maxindex = egkey[key].maxindex; |
| 1682 | |
| 1683 | |
| 1684 | |
| 1685 | assert (Uncompressed); |
| 1686 | assert (side == WH || side == BL); |
| 1687 | assert (out_dtm != NULL); |
| 1688 | assert (idx >= 0); |
| 1689 | assert (key < MAX_EGKEYS); |
| 1690 | |
| 1691 | |
| 1692 | #if defined(USE_FD) |
| 1693 | if (NULL == (finp = egkey[key].fd) ) { |
| 1694 | if (NULL == (finp = fd_openit (key))) { |
| 1695 | return FALSE; |
| 1696 | } |
| 1697 | } |
| 1698 | #else |
| 1699 | sprintf (buf, "%s.gtb", egkey[key].str); |
| 1700 | if (NULL == (finp = fopen (filename, "rb"))) { |
| 1701 | return FALSE; |
| 1702 | } |
| 1703 | #endif |
| 1704 | |
| 1705 | ok = fpark_entry_packed (finp, side, maxindex, idx); |
| 1706 | ok = ok && fread_entry_packed (finp, side, &x); |
| 1707 | |
| 1708 | if (ok) { |
| 1709 | *out_dtm = x; |
| 1710 | } else |
| 1711 | *out_dtm = iFORBID; |
| 1712 | |
| 1713 | #if !defined(USE_FD) |
| 1714 | fclose (finp); |
| 1715 | #endif |
| 1716 | |
| 1717 | return ok; |
| 1718 | } |
| 1719 | |
| 1720 | /* will get defined later */ |
| 1721 | static bool_t dtm_cache_is_on (void); |
| 1722 | |
| 1723 | static bool_t |
| 1724 | egtb_get_dtm (tbkey_t k, unsigned stm, const SQUARE *wS, const SQUARE *bS, bool_t probe_hard_flag, dtm_t *dtm) |
| 1725 | { |
| 1726 | bool_t idxavail; |
| 1727 | index_t idx; |
| 1728 | dtm_t *tab[2]; |
| 1729 | bool_t (*pc2idx) (const SQUARE *, const SQUARE *, index_t *); |
| 1730 | |
| 1731 | FOLLOW_label("egtb_get_dtm --> starts") |
| 1732 | |
| 1733 | if (egkey[k].status == STATUS_MALLOC || egkey[k].status == STATUS_STATICRAM) { |
| 1734 | |
| 1735 | tab[WH] = egkey[k].egt_w; |
| 1736 | tab[BL] = egkey[k].egt_b; |
| 1737 | pc2idx = egkey[k].pctoi; |
| 1738 | |
| 1739 | idxavail = pc2idx (wS, bS, &idx); |
| 1740 | |
| 1741 | FOLLOW_LU("indexavail (RAM)",idxavail) |
| 1742 | |
| 1743 | if (idxavail) { |
| 1744 | *dtm = tab[stm][idx]; |
| 1745 | } else { |
| 1746 | *dtm = iFORBID; |
| 1747 | } |
| 1748 | |
| 1749 | return TRUE; |
| 1750 | |
| 1751 | } else if (egkey[k].status == STATUS_ABSENT) { |
| 1752 | |
| 1753 | pc2idx = egkey[k].pctoi; |
| 1754 | idxavail = pc2idx (wS, bS, &idx); |
| 1755 | |
| 1756 | FOLLOW_LU("indexavail (HD)",idxavail) |
| 1757 | |
| 1758 | if (idxavail) { |
| 1759 | bool_t success; |
| 1760 | |
| 1761 | /* |
| 1762 | | LOCK |
| 1763 | *-------------------------------*/ |
| 1764 | mythread_mutex_lock (&Egtb_lock); |
| 1765 | |
| 1766 | if (dtm_cache_is_on()) { |
| 1767 | |
| 1768 | success = get_dtm (k, stm, idx, dtm, probe_hard_flag); |
| 1769 | |
| 1770 | FOLLOW_LU("get_dtm (succ)",success) |
| 1771 | FOLLOW_LU("get_dtm (dtm )",*dtm) |
| 1772 | |
| 1773 | #if defined(DEBUG) |
| 1774 | if (Uncompressed) { |
| 1775 | dtm_t dtm_temp; |
| 1776 | bool_t ok; |
| 1777 | bool_t success2; |
| 1778 | |
| 1779 | assert (decoding_scheme() == 0 && GTB_scheme == 0); |
| 1780 | |
| 1781 | success2 = egtb_filepeek (k, stm, idx, &dtm_temp); |
| 1782 | ok = (success == success2) && (!success || *dtm == dtm_temp); |
| 1783 | if (!ok) { |
| 1784 | printf ("\nERROR\nsuccess1=%d sucess2=%d\n" |
| 1785 | "k=%d stm=%u idx=%d dtm_peek=%d dtm_cache=%d\n", |
| 1786 | success, success2, k, stm, idx, dtm_temp, *dtm); |
| 1787 | fatal_error(); |
| 1788 | } |
| 1789 | } |
| 1790 | #endif |
| 1791 | |
| 1792 | } else { |
| 1793 | assert(Uncompressed); |
| 1794 | if (probe_hard_flag && Uncompressed) |
| 1795 | success = egtb_filepeek (k, stm, idx, dtm); |
| 1796 | else |
| 1797 | success = FALSE; |
| 1798 | } |
| 1799 | |
| 1800 | mythread_mutex_unlock (&Egtb_lock); |
| 1801 | /*------------------------------*\ |
| 1802 | | UNLOCK |
| 1803 | */ |
| 1804 | |
| 1805 | |
| 1806 | if (success) { |
| 1807 | return TRUE; |
| 1808 | } else { |
| 1809 | if (probe_hard_flag) /* after probing hard and failing, no chance to succeed later */ |
| 1810 | egkey[k].status = STATUS_REJECT; |
| 1811 | *dtm = iUNKNOWN; |
| 1812 | return FALSE; |
| 1813 | } |
| 1814 | |
| 1815 | } else { |
| 1816 | *dtm = iFORBID; |
| 1817 | return TRUE; |
| 1818 | } |
| 1819 | |
| 1820 | } else if (egkey[k].status == STATUS_REJECT) { |
| 1821 | |
| 1822 | FOLLOW_label("STATUS_REJECT") |
| 1823 | |
| 1824 | *dtm = iFORBID; |
| 1825 | return FALSE; |
| 1826 | } else { |
| 1827 | |
| 1828 | FOLLOW_label("STATUS_WRONG!") |
| 1829 | |
| 1830 | assert(0); |
| 1831 | *dtm = iFORBID; |
| 1832 | return FALSE; |
| 1833 | } |
| 1834 | |
| 1835 | } |
| 1836 | |
| 1837 | static void |
| 1838 | removepiece (SQUARE *ys, SQ_CONTENT *yp, int j) |
| 1839 | { |
| 1840 | int k; |
| 1841 | for (k = j; ys[k] != NOSQUARE; k++) { |
| 1842 | ys[k] = ys[k+1]; |
| 1843 | yp[k] = yp[k+1]; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /* |
| 1848 | | |
| 1849 | | mySHARED by probe and build |
| 1850 | | |
| 1851 | \*----------------------------------------------------*/ |
| 1852 | |
| 1853 | mySHARED /*@NULL@*/ FILE * |
| 1854 | fd_openit (tbkey_t key) |
| 1855 | { |
| 1856 | int i; |
| 1857 | tbkey_t closingkey; |
| 1858 | FILE * finp = NULL; |
| 1859 | char buf[4096]; |
| 1860 | char * filename = buf; |
| 1861 | int start; |
| 1862 | int end; |
| 1863 | int pth; |
| 1864 | const char * extension; |
| 1865 | |
| 1866 | assert (0 <= key && key < MAX_EGKEYS); |
| 1867 | assert (0 <= fd.n && fd.n <= GTB_MAXOPEN); |
| 1868 | |
| 1869 | /* test if I reach limit of files open */ |
| 1870 | if (fd.n == GTB_MAXOPEN) { |
| 1871 | |
| 1872 | /* fclose the last accessed, first in the list */ |
| 1873 | closingkey = fd.key[0]; |
| 1874 | finp = egkey [closingkey].fd; |
| 1875 | assert (finp != NULL); |
| 1876 | fclose (finp); |
| 1877 | egkey[closingkey].fd = NULL; |
| 1878 | finp = NULL; |
| 1879 | |
| 1880 | for (i = 1; i < fd.n; i++) { |
| 1881 | fd.key[i-1] = fd.key[i]; |
| 1882 | } |
| 1883 | fd.key[--fd.n] = -1; |
| 1884 | } |
| 1885 | |
| 1886 | assert (fd.n < GTB_MAXOPEN); |
| 1887 | |
| 1888 | /* set proper extensions to the File */ |
| 1889 | if (Uncompressed) { |
| 1890 | assert (decoding_scheme() == 0 && GTB_scheme == 0); |
| 1891 | extension = ".gtb"; |
| 1892 | } else { |
| 1893 | extension = Extension[decoding_scheme()]; |
| 1894 | } |
| 1895 | |
| 1896 | /* Scan folders to find the File*/ |
| 1897 | finp = NULL; |
| 1898 | |
| 1899 | start = egkey[key].pathn; |
| 1900 | end = Gtbpath_end_index; |
| 1901 | |
| 1902 | /*@@ |
| 1903 | printf ("start: %d\n",start); |
| 1904 | printf ("===================Gtbpath[0]=%s\n",Gtbpath[0]); |
| 1905 | */ |
| 1906 | for (pth = start; NULL == finp && pth < end && Gtbpath[pth] != NULL; pth++) { |
| 1907 | const char *path = Gtbpath[pth]; |
| 1908 | size_t pl = strlen(path); |
| 1909 | /*@@ |
| 1910 | printf ("path: %s\n",path); |
| 1911 | */ |
| 1912 | if (pl == 0) { |
| 1913 | sprintf (buf, "%s%s%s", path, egkey[key].str, extension); |
| 1914 | } else { |
| 1915 | if (isfoldersep( path[pl-1] )) { |
| 1916 | sprintf (buf, "%s%s%s", path, egkey[key].str, extension); |
| 1917 | } else { |
| 1918 | sprintf (buf, "%s%s%s%s", path, FOLDERSEP, egkey[key].str, extension); |
| 1919 | } |
| 1920 | } |
| 1921 | /*printf ("try to open %s --> ",filename);*/ |
| 1922 | /* Finally found the file? */ |
| 1923 | finp = fopen (filename, "rb"); |
| 1924 | /*printf ("%d\n",finp != NULL);*/ |
| 1925 | } |
| 1926 | |
| 1927 | /* File was found and opened */ |
| 1928 | if (NULL != finp) { |
| 1929 | fd.key [fd.n++] = key; |
| 1930 | egkey[key].fd = finp; |
| 1931 | egkey[key].pathn = pth; /* remember succesful path */ |
| 1932 | eg_was_open[key] = 1; |
| 1933 | return finp; |
| 1934 | } |
| 1935 | |
| 1936 | start = 0; |
| 1937 | end = egkey[key].pathn; |
| 1938 | for (pth = start; NULL == finp && pth < end && Gtbpath[pth] != NULL; pth++) { |
| 1939 | const char *path = Gtbpath[pth]; |
| 1940 | size_t pl = strlen(path); |
| 1941 | |
| 1942 | if (pl == 0) { |
| 1943 | sprintf (buf, "%s%s%s", path, egkey[key].str, extension); |
| 1944 | } else { |
| 1945 | if (isfoldersep( path[pl-1] )) { |
| 1946 | sprintf (buf, "%s%s%s", path, egkey[key].str, extension); |
| 1947 | } else { |
| 1948 | sprintf (buf, "%s%s%s%s", path, FOLDERSEP, egkey[key].str, extension); |
| 1949 | } |
| 1950 | } |
| 1951 | /*printf ("try to open %s --> ",filename);*/ |
| 1952 | /* Finally found the file? */ |
| 1953 | finp = fopen (filename, "rb"); |
| 1954 | /*printf ("%d\n",finp != NULL);*/ |
| 1955 | } |
| 1956 | |
| 1957 | |
| 1958 | /* File was found and opened */ |
| 1959 | if (NULL != finp) { |
| 1960 | fd.key [fd.n++] = key; |
| 1961 | egkey[key].fd = finp; |
| 1962 | egkey[key].pathn = pth; /* remember succesful path */ |
| 1963 | eg_was_open[key] = 1; |
| 1964 | } |
| 1965 | |
| 1966 | return finp; |
| 1967 | } |
| 1968 | |
| 1969 | #ifdef _MSC_VER |
| 1970 | /* to silence warning for sprintf usage */ |
| 1971 | #pragma warning(default:4996) |
| 1972 | #endif |
| 1973 | |
| 1974 | mySHARED void |
| 1975 | sortlists (SQUARE *ws, SQ_CONTENT *wp) |
| 1976 | { |
| 1977 | int i, j; |
| 1978 | SQUARE ts; |
| 1979 | SQ_CONTENT tp; |
| 1980 | /* input is sorted */ |
| 1981 | for (i = 0; wp[i] != NOPIECE; i++) { |
| 1982 | for (j = (i+1); wp[j] != NOPIECE; j++) { |
| 1983 | if (wp[j] > wp[i]) { |
| 1984 | tp = wp[i]; wp[i] = wp[j]; wp[j] = tp; |
| 1985 | ts = ws[i]; ws[i] = ws[j]; ws[j] = ts; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | mySHARED void |
| 1992 | list_sq_copy (const SQUARE *a, SQUARE *b) |
| 1993 | { |
| 1994 | while (NOSQUARE != (*b++ = *a++)) |
| 1995 | ; |
| 1996 | } |
| 1997 | |
| 1998 | mySHARED void |
| 1999 | list_pc_copy (const SQ_CONTENT *a, SQ_CONTENT *b) |
| 2000 | { |
| 2001 | while (NOPIECE != (*b++ = *a++)) |
| 2002 | ; |
| 2003 | } |
| 2004 | |
| 2005 | mySHARED dtm_t |
| 2006 | inv_dtm (dtm_t x) |
| 2007 | { |
| 2008 | unsigned mat; |
| 2009 | assert ( (x & iUNKNBIT) == 0); |
| 2010 | |
| 2011 | if (x == iDRAW || x == iFORBID) |
| 2012 | return x; |
| 2013 | |
| 2014 | mat = (unsigned)x & 3u; |
| 2015 | if (mat == iWMATE) |
| 2016 | mat = iBMATE; |
| 2017 | else |
| 2018 | mat = iWMATE; |
| 2019 | |
| 2020 | x = (dtm_t) (((unsigned)x & ~3u) | mat); |
| 2021 | |
| 2022 | return x; |
| 2023 | } |
| 2024 | |
| 2025 | static const char pctoch[] = {'-','p','n','b','r','q','k'}; |
| 2026 | |
| 2027 | mySHARED bool_t |
| 2028 | egtb_get_id (SQ_CONTENT *w, SQ_CONTENT *b, tbkey_t *id) |
| 2029 | { |
| 2030 | |
| 2031 | char pcstr[2*MAX_LISTSIZE]; |
| 2032 | SQ_CONTENT *s; |
| 2033 | char *t; |
| 2034 | bool_t found; |
| 2035 | tbkey_t i; |
| 2036 | static tbkey_t cache_i = 0; |
| 2037 | |
| 2038 | assert (PAWN == 1 && KNIGHT == 2 && BISHOP == 3 && ROOK == 4 && QUEEN == 5 && KING == 6); |
| 2039 | |
| 2040 | t = pcstr; |
| 2041 | |
| 2042 | s = w; |
| 2043 | while (NOPIECE != *s) |
| 2044 | *t++ = pctoch[*s++]; |
| 2045 | s = b; |
| 2046 | while (NOPIECE != *s) |
| 2047 | *t++ = pctoch[*s++]; |
| 2048 | |
| 2049 | *t = '\0'; |
| 2050 | |
| 2051 | found = (0 == strcmp(pcstr, egkey[cache_i].str)); |
| 2052 | if (found) { |
| 2053 | *id = cache_i; |
| 2054 | return found; |
| 2055 | } |
| 2056 | |
| 2057 | for (i = 0, found = FALSE; !found && egkey[i].str != NULL; i++) { |
| 2058 | found = (0 == strcmp(pcstr, egkey[i].str)); |
| 2059 | } |
| 2060 | if (found) { |
| 2061 | cache_i = *id = i - 1; |
| 2062 | } |
| 2063 | |
| 2064 | return found; |
| 2065 | } |
| 2066 | |
| 2067 | mySHARED void |
| 2068 | list_sq_flipNS (SQUARE *s) |
| 2069 | { |
| 2070 | while (*s != NOSQUARE) { |
| 2071 | *s ^= 070; |
| 2072 | s++; |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | mySHARED void |
| 2077 | unpackdist (dtm_t d, unsigned int *res, unsigned int *ply) |
| 2078 | { |
| 2079 | *ply = (unsigned int)d >> PLYSHIFT; |
| 2080 | *res = d & INFOMASK; |
| 2081 | } |
| 2082 | |
| 2083 | mySHARED dtm_t |
| 2084 | packdist (unsigned int inf, unsigned int ply) |
| 2085 | { |
| 2086 | assert (inf <= INFOMASK); |
| 2087 | return (dtm_t) (inf | ply << PLYSHIFT); |
| 2088 | } |
| 2089 | |
| 2090 | mySHARED dtm_t |
| 2091 | adjust_up (dtm_t dist) |
| 2092 | { |
| 2093 | #if 0 |
| 2094 | static const dtm_t adding[] = { |
| 2095 | 0, 1<<PLYSHIFT, 1<<PLYSHIFT, 0, |
| 2096 | 0, 1<<PLYSHIFT, 1<<PLYSHIFT, 0 |
| 2097 | }; |
| 2098 | dist += adding [dist&INFOMASK]; |
| 2099 | return dist; |
| 2100 | #else |
| 2101 | unsigned udist = (unsigned) dist; |
| 2102 | switch (udist & INFOMASK) { |
| 2103 | case iWMATE: |
| 2104 | case iWMATEt: |
| 2105 | case iBMATE: |
| 2106 | case iBMATEt: |
| 2107 | udist += (1u << PLYSHIFT); |
| 2108 | break; |
| 2109 | default: |
| 2110 | break; |
| 2111 | } |
| 2112 | return (dtm_t) udist; |
| 2113 | #endif |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | mySHARED dtm_t |
| 2118 | bestx (unsigned stm, dtm_t a, dtm_t b) |
| 2119 | { |
| 2120 | unsigned int key; |
| 2121 | static const unsigned int |
| 2122 | comparison [4] [4] = { |
| 2123 | /*draw, wmate, bmate, forbid*/ |
| 2124 | /* draw */ {0, 3, 0, 0}, |
| 2125 | /* wmate */ {0, 1, 0, 0}, |
| 2126 | /* bmate */ {3, 3, 2, 0}, |
| 2127 | /* forbid*/ {3, 3, 3, 0} |
| 2128 | |
| 2129 | /* 0 = selectfirst */ |
| 2130 | /* 1 = selectlowest */ |
| 2131 | /* 2 = selecthighest */ |
| 2132 | /* 3 = selectsecond */ |
| 2133 | }; |
| 2134 | |
| 2135 | static const unsigned int xorkey [2] = {0, 3}; |
| 2136 | dtm_t retu[4]; |
| 2137 | dtm_t ret = iFORBID; |
| 2138 | |
| 2139 | assert (stm == WH || stm == BL); |
| 2140 | assert ((a & iUNKNBIT) == 0 && (b & iUNKNBIT) == 0 ); |
| 2141 | |
| 2142 | if (a == iFORBID) |
| 2143 | return b; |
| 2144 | if (b == iFORBID) |
| 2145 | return a; |
| 2146 | |
| 2147 | retu[0] = a; /* first parameter */ |
| 2148 | retu[1] = a; /* the lowest by default */ |
| 2149 | retu[2] = b; /* highest by default */ |
| 2150 | retu[3] = b; /* second parameter */ |
| 2151 | if (b < a) { |
| 2152 | retu[1] = b; |
| 2153 | retu[2] = a; |
| 2154 | } |
| 2155 | |
| 2156 | key = comparison [a&3] [b&3] ^ xorkey[stm]; |
| 2157 | ret = retu [key]; |
| 2158 | |
| 2159 | return ret; |
| 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | /*--------------------------------------------------------------------------*\ |
| 2164 | | PACKING ZONE |
| 2165 | *--------------------------------------------------------------------------*/ |
| 2166 | |
| 2167 | mySHARED dtm_t |
| 2168 | dtm_unpack (unsigned stm, unsigned char packed) |
| 2169 | { |
| 2170 | unsigned int info, plies, prefx, store, moves; |
| 2171 | dtm_t ret; |
| 2172 | unsigned int p = packed; |
| 2173 | |
| 2174 | if (iDRAW == p || iFORBID == p) { |
| 2175 | return (dtm_t) p; |
| 2176 | } |
| 2177 | |
| 2178 | info = (unsigned int) p & 3; |
| 2179 | store = (unsigned int) p >> 2; |
| 2180 | |
| 2181 | if (WH == stm) { |
| 2182 | switch (info) { |
| 2183 | case iWMATE: |
| 2184 | moves = store + 1; |
| 2185 | plies = moves * 2 - 1; |
| 2186 | prefx = info; |
| 2187 | break; |
| 2188 | |
| 2189 | case iBMATE: |
| 2190 | moves = store; |
| 2191 | plies = moves * 2; |
| 2192 | prefx = info; |
| 2193 | break; |
| 2194 | |
| 2195 | case iDRAW: |
| 2196 | moves = store + 1 + 63; |
| 2197 | plies = moves * 2 - 1; |
| 2198 | prefx = iWMATE; |
| 2199 | break; |
| 2200 | |
| 2201 | case iFORBID: |
| 2202 | |
| 2203 | moves = store + 63; |
| 2204 | plies = moves * 2; |
| 2205 | prefx = iBMATE; |
| 2206 | break; |
| 2207 | default: |
| 2208 | plies = 0; |
| 2209 | prefx = 0; |
| 2210 | assert(0); |
| 2211 | break; |
| 2212 | |
| 2213 | } |
| 2214 | ret = (dtm_t) (prefx | (plies << 3)); |
| 2215 | } else { |
| 2216 | switch (info) { |
| 2217 | case iBMATE: |
| 2218 | moves = store + 1; |
| 2219 | plies = moves * 2 - 1; |
| 2220 | prefx = info; |
| 2221 | break; |
| 2222 | |
| 2223 | case iWMATE: |
| 2224 | moves = store; |
| 2225 | plies = moves * 2; |
| 2226 | prefx = info; |
| 2227 | break; |
| 2228 | |
| 2229 | case iDRAW: |
| 2230 | |
| 2231 | if (store == 63) { |
| 2232 | /* exception: no position in the 5-man |
| 2233 | TBs needs to store 63 for iBMATE |
| 2234 | it is then used to indicate iWMATE |
| 2235 | when just overflows */ |
| 2236 | store++; |
| 2237 | |
| 2238 | moves = store + 63; |
| 2239 | plies = moves * 2; |
| 2240 | prefx = iWMATE; |
| 2241 | |
| 2242 | break; |
| 2243 | } |
| 2244 | |
| 2245 | moves = store + 1 + 63; |
| 2246 | plies = moves * 2 - 1; |
| 2247 | prefx = iBMATE; |
| 2248 | break; |
| 2249 | |
| 2250 | case iFORBID: |
| 2251 | |
| 2252 | moves = store + 63; |
| 2253 | plies = moves * 2; |
| 2254 | prefx = iWMATE; |
| 2255 | break; |
| 2256 | default: |
| 2257 | plies = 0; |
| 2258 | prefx = 0; |
| 2259 | assert(0); |
| 2260 | break; |
| 2261 | |
| 2262 | } |
| 2263 | ret = (dtm_t) (prefx | (plies << 3)); |
| 2264 | } |
| 2265 | return ret; |
| 2266 | } |
| 2267 | |
| 2268 | |
| 2269 | /* |
| 2270 | static bool_t fwrite_entry_packed (FILE *dest, unsigned side, dtm_t x); |
| 2271 | */ |
| 2272 | |
| 2273 | |
| 2274 | mySHARED bool_t |
| 2275 | fread_entry_packed (FILE *finp, unsigned side, dtm_t *px) |
| 2276 | { |
| 2277 | unsigned char p[SLOTSIZE]; |
| 2278 | bool_t ok = (SLOTSIZE == fread (p, sizeof(unsigned char), SLOTSIZE, finp)); |
| 2279 | if (ok) { |
| 2280 | *px = dtm_unpack (side, p[0]); |
| 2281 | } |
| 2282 | return ok; |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | mySHARED bool_t |
| 2287 | fpark_entry_packed (FILE *finp, unsigned side, index_t max, index_t idx) |
| 2288 | { |
| 2289 | bool_t ok; |
| 2290 | index_t i; |
| 2291 | long int fseek_i; |
| 2292 | index_t sz = (index_t) sizeof(unsigned char); |
| 2293 | |
| 2294 | assert (side == WH || side == BL); |
| 2295 | assert (finp != NULL); |
| 2296 | assert (idx >= 0); |
| 2297 | i = ((index_t)side * max + idx) * sz; |
| 2298 | fseek_i = (long int) i; |
| 2299 | assert (fseek_i >= 0); |
| 2300 | ok = (0 == fseek (finp, fseek_i, SEEK_SET)); |
| 2301 | return ok; |
| 2302 | } |
| 2303 | |
| 2304 | /*----------------------------------------------------*\ |
| 2305 | | |
| 2306 | | shared by probe and build |
| 2307 | | |
| 2308 | \*/ |
| 2309 | |
| 2310 | |
| 2311 | /*---------------------------------------------------------------------*\ |
| 2312 | | WDL CACHE Implementation ZONE |
| 2313 | \*---------------------------------------------------------------------*/ |
| 2314 | |
| 2315 | #define WDL_entries_per_unit 4 |
| 2316 | #define WDL_entry_mask 3 |
| 2317 | static size_t WDL_units_per_block = 0; |
| 2318 | |
| 2319 | static bool_t WDL_CACHE_INITIALIZED = FALSE; |
| 2320 | |
| 2321 | typedef unsigned char unit_t; /* block unit */ |
| 2322 | |
| 2323 | typedef struct wdl_block wdl_block_t; |
| 2324 | |
| 2325 | struct wdl_block { |
| 2326 | tbkey_t key; |
| 2327 | unsigned side; |
| 2328 | index_t offset; |
| 2329 | unit_t *p_arr; |
| 2330 | wdl_block_t *prev; |
| 2331 | wdl_block_t *next; |
| 2332 | }; |
| 2333 | |
| 2334 | struct WDL_CACHE { |
| 2335 | /* defined at init */ |
| 2336 | bool_t cached; |
| 2337 | size_t max_blocks; |
| 2338 | size_t entries_per_block; |
| 2339 | unit_t * buffer; |
| 2340 | |
| 2341 | /* flushables */ |
| 2342 | wdl_block_t * top; |
| 2343 | wdl_block_t * bot; |
| 2344 | size_t n; |
| 2345 | wdl_block_t * blocks; /* was entry */ |
| 2346 | |
| 2347 | /* counters */ |
| 2348 | uint64_t hard; |
| 2349 | uint64_t soft; |
| 2350 | uint64_t hardmisses; |
| 2351 | uint64_t hits; |
| 2352 | uint64_t softmisses; |
| 2353 | uint64_t comparisons; |
| 2354 | }; |
| 2355 | |
| 2356 | struct WDL_CACHE wdl_cache = {FALSE,0,0,NULL, |
| 2357 | NULL,NULL,0,NULL, |
| 2358 | 0,0,0,0,0,0}; |
| 2359 | |
| 2360 | |
| 2361 | /*---------------------------------------------------------------------*\ |
| 2362 | | DTM CACHE Implementation ZONE |
| 2363 | \*---------------------------------------------------------------------*/ |
| 2364 | |
| 2365 | struct dtm_block; |
| 2366 | |
| 2367 | typedef struct dtm_block dtm_block_t; |
| 2368 | |
| 2369 | struct dtm_block { |
| 2370 | tbkey_t key; |
| 2371 | unsigned side; |
| 2372 | index_t offset; |
| 2373 | dtm_t *p_arr; |
| 2374 | dtm_block_t *prev; |
| 2375 | dtm_block_t *next; |
| 2376 | }; |
| 2377 | |
| 2378 | struct cache_table { |
| 2379 | /* defined at init */ |
| 2380 | bool_t cached; |
| 2381 | size_t max_blocks; |
| 2382 | size_t entries_per_block; |
| 2383 | dtm_t * buffer; |
| 2384 | |
| 2385 | /* flushables */ |
| 2386 | dtm_block_t * top; |
| 2387 | dtm_block_t * bot; |
| 2388 | size_t n; |
| 2389 | dtm_block_t * entry; |
| 2390 | |
| 2391 | /* counters */ |
| 2392 | uint64_t hard; |
| 2393 | uint64_t soft; |
| 2394 | uint64_t hardmisses; |
| 2395 | uint64_t hits; |
| 2396 | uint64_t softmisses; |
| 2397 | unsigned long comparisons; |
| 2398 | }; |
| 2399 | |
| 2400 | struct cache_table dtm_cache = {FALSE,0,0,NULL, |
| 2401 | NULL,NULL,0,NULL, |
| 2402 | 0,0,0,0,0,0}; |
| 2403 | |
| 2404 | struct general_counters { |
| 2405 | /* counters */ |
| 2406 | uint64_t hits; |
| 2407 | uint64_t miss; |
| 2408 | }; |
| 2409 | |
| 2410 | static struct general_counters Drive = {0,0}; |
| 2411 | |
| 2412 | |
| 2413 | static void split_index (size_t entries_per_block, index_t i, index_t *o, index_t *r); |
| 2414 | static dtm_block_t *point_block_to_replace (void); |
| 2415 | static bool_t preload_cache (tbkey_t key, unsigned side, index_t idx); |
| 2416 | static void movetotop (dtm_block_t *t); |
| 2417 | |
| 2418 | /*--cache prototypes--------------------------------------------------------*/ |
| 2419 | |
| 2420 | /*- WDL --------------------------------------------------------------------*/ |
| 2421 | #ifdef WDL_PROBE |
| 2422 | static unsigned int wdl_extract (unit_t *uarr, index_t x); |
| 2423 | static wdl_block_t * wdl_point_block_to_replace (void); |
| 2424 | static void wdl_movetotop (wdl_block_t *t); |
| 2425 | |
| 2426 | #if 0 |
| 2427 | static bool_t wdl_cache_init (size_t cache_mem); |
| 2428 | static void wdl_cache_flush (void); |
| 2429 | static bool_t get_WDL (tbkey_t key, unsigned side, index_t idx, unsigned int *info_out, bool_t probe_hard_flag); |
| 2430 | #endif |
| 2431 | |
| 2432 | static bool_t wdl_cache_is_on (void); |
| 2433 | static void wdl_cache_reset_counters (void); |
| 2434 | static void wdl_cache_done (void); |
| 2435 | |
| 2436 | static wdl_block_t * wdl_point_block_to_replace (void); |
| 2437 | static bool_t get_WDL_from_cache (tbkey_t key, unsigned side, index_t idx, unsigned int *out); |
| 2438 | static void wdl_movetotop (wdl_block_t *t); |
| 2439 | static bool_t wdl_preload_cache (tbkey_t key, unsigned side, index_t idx); |
| 2440 | #endif |
| 2441 | /*--------------------------------------------------------------------------*/ |
| 2442 | /*- DTM --------------------------------------------------------------------*/ |
| 2443 | static bool_t dtm_cache_is_on (void); |
| 2444 | static void dtm_cache_reset_counters (void); |
| 2445 | static void dtm_cache_done (void); |
| 2446 | |
| 2447 | static size_t dtm_cache_init (size_t cache_mem); |
| 2448 | static void dtm_cache_flush (void); |
| 2449 | /*--------------------------------------------------------------------------*/ |
| 2450 | |
| 2451 | static bool_t |
| 2452 | dtm_cache_is_on (void) |
| 2453 | { |
| 2454 | return dtm_cache.cached; |
| 2455 | } |
| 2456 | |
| 2457 | static void |
| 2458 | dtm_cache_reset_counters (void) |
| 2459 | { |
| 2460 | dtm_cache.hard = 0; |
| 2461 | dtm_cache.soft = 0; |
| 2462 | dtm_cache.hardmisses = 0; |
| 2463 | dtm_cache.hits = 0; |
| 2464 | dtm_cache.softmisses = 0; |
| 2465 | dtm_cache.comparisons = 0; |
| 2466 | return; |
| 2467 | } |
| 2468 | |
| 2469 | |
| 2470 | static size_t |
| 2471 | dtm_cache_init (size_t cache_mem) |
| 2472 | { |
| 2473 | unsigned int i; |
| 2474 | dtm_block_t *p; |
| 2475 | size_t entries_per_block; |
| 2476 | size_t max_blocks; |
| 2477 | size_t block_mem; |
| 2478 | |
| 2479 | if (DTM_CACHE_INITIALIZED) |
| 2480 | dtm_cache_done(); |
| 2481 | |
| 2482 | entries_per_block = 16 * 1024; /* fixed, needed for the compression schemes */ |
| 2483 | |
| 2484 | block_mem = entries_per_block * sizeof(dtm_t); |
| 2485 | |
| 2486 | max_blocks = cache_mem / block_mem; |
| 2487 | if (!Uncompressed && 1 > max_blocks) |
| 2488 | max_blocks = 1; |
| 2489 | cache_mem = max_blocks * block_mem; |
| 2490 | |
| 2491 | |
| 2492 | dtm_cache_reset_counters (); |
| 2493 | |
| 2494 | dtm_cache.entries_per_block = entries_per_block; |
| 2495 | dtm_cache.max_blocks = max_blocks; |
| 2496 | dtm_cache.cached = TRUE; |
| 2497 | dtm_cache.top = NULL; |
| 2498 | dtm_cache.bot = NULL; |
| 2499 | dtm_cache.n = 0; |
| 2500 | |
| 2501 | if (0 == cache_mem || NULL == (dtm_cache.buffer = (dtm_t *) malloc (cache_mem))) { |
| 2502 | dtm_cache.cached = FALSE; |
| 2503 | dtm_cache.buffer = NULL; |
| 2504 | dtm_cache.entry = NULL; |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
| 2508 | if (0 == max_blocks|| NULL == (dtm_cache.entry = (dtm_block_t *) malloc (max_blocks * sizeof(dtm_block_t)))) { |
| 2509 | dtm_cache.cached = FALSE; |
| 2510 | dtm_cache.entry = NULL; |
| 2511 | free (dtm_cache.buffer); |
| 2512 | dtm_cache.buffer = NULL; |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
| 2516 | for (i = 0; i < max_blocks; i++) { |
| 2517 | p = &dtm_cache.entry[i]; |
| 2518 | p->key = -1; |
| 2519 | p->side = gtbNOSIDE; |
| 2520 | p->offset = gtbNOINDEX; |
| 2521 | p->p_arr = dtm_cache.buffer + i * entries_per_block; |
| 2522 | p->prev = NULL; |
| 2523 | p->next = NULL; |
| 2524 | } |
| 2525 | |
| 2526 | DTM_CACHE_INITIALIZED = TRUE; |
| 2527 | |
| 2528 | return cache_mem; |
| 2529 | } |
| 2530 | |
| 2531 | |
| 2532 | static void |
| 2533 | dtm_cache_done (void) |
| 2534 | { |
| 2535 | assert(DTM_CACHE_INITIALIZED); |
| 2536 | |
| 2537 | dtm_cache.cached = FALSE; |
| 2538 | dtm_cache.hard = 0; |
| 2539 | dtm_cache.soft = 0; |
| 2540 | dtm_cache.hardmisses = 0; |
| 2541 | dtm_cache.hits = 0; |
| 2542 | dtm_cache.softmisses = 0; |
| 2543 | dtm_cache.comparisons = 0; |
| 2544 | dtm_cache.max_blocks = 0; |
| 2545 | dtm_cache.entries_per_block = 0; |
| 2546 | |
| 2547 | dtm_cache.top = NULL; |
| 2548 | dtm_cache.bot = NULL; |
| 2549 | dtm_cache.n = 0; |
| 2550 | |
| 2551 | if (dtm_cache.buffer != NULL) |
| 2552 | free (dtm_cache.buffer); |
| 2553 | dtm_cache.buffer = NULL; |
| 2554 | |
| 2555 | if (dtm_cache.entry != NULL) |
| 2556 | free (dtm_cache.entry); |
| 2557 | dtm_cache.entry = NULL; |
| 2558 | |
| 2559 | DTM_CACHE_INITIALIZED = FALSE; |
| 2560 | |
| 2561 | return; |
| 2562 | } |
| 2563 | |
| 2564 | static void |
| 2565 | dtm_cache_flush (void) |
| 2566 | { |
| 2567 | unsigned int i; |
| 2568 | dtm_block_t *p; |
| 2569 | size_t entries_per_block = dtm_cache.entries_per_block; |
| 2570 | size_t max_blocks = dtm_cache.max_blocks; |
| 2571 | |
| 2572 | dtm_cache.top = NULL; |
| 2573 | dtm_cache.bot = NULL; |
| 2574 | dtm_cache.n = 0; |
| 2575 | |
| 2576 | for (i = 0; i < max_blocks; i++) { |
| 2577 | p = &dtm_cache.entry[i]; |
| 2578 | p->key = -1; |
| 2579 | p->side = gtbNOSIDE; |
| 2580 | p->offset = gtbNOINDEX; |
| 2581 | p->p_arr = dtm_cache.buffer + i * entries_per_block; |
| 2582 | p->prev = NULL; |
| 2583 | p->next = NULL; |
| 2584 | } |
| 2585 | dtm_cache_reset_counters (); |
| 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | /*---- end tbcache zone ----------------------------------------------------------------------*/ |
| 2591 | |
| 2592 | extern bool_t |
| 2593 | tbcache_is_on (void) |
| 2594 | { |
| 2595 | return dtm_cache_is_on() || wdl_cache_is_on(); |
| 2596 | } |
| 2597 | |
| 2598 | |
| 2599 | /* STATISTICS OUTPUT */ |
| 2600 | |
| 2601 | extern void |
| 2602 | tbstats_get (struct TB_STATS *x) |
| 2603 | { |
| 2604 | long unsigned mask = 0xfffffffflu; |
| 2605 | uint64_t memory_hits, total_hits; |
| 2606 | |
| 2607 | |
| 2608 | /* |
| 2609 | | WDL CACHE |
| 2610 | \*---------------------------------------------------*/ |
| 2611 | |
| 2612 | x->wdl_easy_hits[0] = (long unsigned)(wdl_cache.hits & mask); |
| 2613 | x->wdl_easy_hits[1] = (long unsigned)(wdl_cache.hits >> 32); |
| 2614 | |
| 2615 | x->wdl_hard_prob[0] = (long unsigned)(wdl_cache.hard & mask); |
| 2616 | x->wdl_hard_prob[1] = (long unsigned)(wdl_cache.hard >> 32); |
| 2617 | |
| 2618 | x->wdl_soft_prob[0] = (long unsigned)(wdl_cache.soft & mask); |
| 2619 | x->wdl_soft_prob[1] = (long unsigned)(wdl_cache.soft >> 32); |
| 2620 | |
| 2621 | x->wdl_cachesize = WDL_cache_size; |
| 2622 | |
| 2623 | /* occupancy */ |
| 2624 | x->wdl_occupancy = wdl_cache.max_blocks==0? 0:(double)100.0*(double)wdl_cache.n/(double)wdl_cache.max_blocks; |
| 2625 | |
| 2626 | /* |
| 2627 | | DTM CACHE |
| 2628 | \*---------------------------------------------------*/ |
| 2629 | |
| 2630 | x->dtm_easy_hits[0] = (long unsigned)(dtm_cache.hits & mask); |
| 2631 | x->dtm_easy_hits[1] = (long unsigned)(dtm_cache.hits >> 32); |
| 2632 | |
| 2633 | x->dtm_hard_prob[0] = (long unsigned)(dtm_cache.hard & mask); |
| 2634 | x->dtm_hard_prob[1] = (long unsigned)(dtm_cache.hard >> 32); |
| 2635 | |
| 2636 | x->dtm_soft_prob[0] = (long unsigned)(dtm_cache.soft & mask); |
| 2637 | x->dtm_soft_prob[1] = (long unsigned)(dtm_cache.soft >> 32); |
| 2638 | |
| 2639 | x->dtm_cachesize = DTM_cache_size; |
| 2640 | |
| 2641 | /* occupancy */ |
| 2642 | x->dtm_occupancy = dtm_cache.max_blocks==0? 0:(double)100.0*(double)dtm_cache.n/(double)dtm_cache.max_blocks; |
| 2643 | |
| 2644 | /* |
| 2645 | | GENERAL |
| 2646 | \*---------------------------------------------------*/ |
| 2647 | |
| 2648 | /* memory */ |
| 2649 | memory_hits = wdl_cache.hits + dtm_cache.hits; |
| 2650 | x->memory_hits[0] = (long unsigned)(memory_hits & mask); |
| 2651 | x->memory_hits[1] = (long unsigned)(memory_hits >> 32); |
| 2652 | |
| 2653 | /* hard drive */ |
| 2654 | x->drive_hits[0] = (long unsigned)(Drive.hits & mask); |
| 2655 | x->drive_hits[1] = (long unsigned)(Drive.hits >> 32); |
| 2656 | |
| 2657 | x->drive_miss[0] = (long unsigned)(Drive.miss & mask); |
| 2658 | x->drive_miss[1] = (long unsigned)(Drive.miss >> 32); |
| 2659 | |
| 2660 | x->bytes_read[0] = (long unsigned)(Bytes_read & mask); |
| 2661 | x->bytes_read[1] = (long unsigned)(Bytes_read >> 32); |
| 2662 | |
| 2663 | x->files_opened = eg_was_open_count(); |
| 2664 | |
| 2665 | /* total */ |
| 2666 | total_hits = memory_hits + Drive.hits; |
| 2667 | x->total_hits[0] = (long unsigned)(total_hits & mask); |
| 2668 | x->total_hits[1] = (long unsigned)(total_hits >> 32); |
| 2669 | |
| 2670 | /* efficiency */ |
| 2671 | { uint64_t denominator = memory_hits + Drive.hits + Drive.miss; |
| 2672 | x->memory_efficiency = 0==denominator? 0: 100.0 * (double)(memory_hits) / (double)(denominator); |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | |
| 2677 | extern bool_t |
| 2678 | tbcache_init (size_t cache_mem, int wdl_fraction) |
| 2679 | { |
| 2680 | assert (wdl_fraction <= WDL_FRACTION_MAX && wdl_fraction >= 0); |
| 2681 | |
| 2682 | /* defensive against input */ |
| 2683 | if (wdl_fraction > WDL_FRACTION_MAX) wdl_fraction = WDL_FRACTION_MAX; |
| 2684 | if (wdl_fraction < 0) wdl_fraction = 0; |
| 2685 | WDL_FRACTION = wdl_fraction; |
| 2686 | |
| 2687 | DTM_cache_size = (cache_mem/(size_t)WDL_FRACTION_MAX)*(size_t)(WDL_FRACTION_MAX-WDL_FRACTION); |
| 2688 | WDL_cache_size = (cache_mem/(size_t)WDL_FRACTION_MAX)*(size_t) WDL_FRACTION ; |
| 2689 | |
| 2690 | #ifdef WDL_PROBE |
| 2691 | /* returns the actual memory allocated */ |
| 2692 | DTM_cache_size = dtm_cache_init (DTM_cache_size); |
| 2693 | WDL_cache_size = wdl_cache_init (WDL_cache_size); |
| 2694 | #else |
| 2695 | /* returns the actual memory allocated */ |
| 2696 | DTM_cache_size = dtm_cache_init (DTM_cache_size); |
| 2697 | #endif |
| 2698 | tbstats_reset (); |
| 2699 | return TRUE; |
| 2700 | } |
| 2701 | |
| 2702 | extern bool_t |
| 2703 | tbcache_restart (size_t cache_mem, int wdl_fraction) |
| 2704 | { |
| 2705 | return tbcache_init (cache_mem, wdl_fraction); |
| 2706 | } |
| 2707 | |
| 2708 | extern void |
| 2709 | tbcache_done (void) |
| 2710 | { |
| 2711 | dtm_cache_done(); |
| 2712 | #ifdef WDL_PROBE |
| 2713 | wdl_cache_done(); |
| 2714 | #endif |
| 2715 | tbstats_reset (); |
| 2716 | return; |
| 2717 | } |
| 2718 | |
| 2719 | extern void |
| 2720 | tbcache_flush (void) |
| 2721 | { |
| 2722 | dtm_cache_flush(); |
| 2723 | #ifdef WDL_PROBE |
| 2724 | wdl_cache_flush(); |
| 2725 | #endif |
| 2726 | tbstats_reset (); |
| 2727 | return; |
| 2728 | } |
| 2729 | |
| 2730 | extern void |
| 2731 | tbstats_reset (void) |
| 2732 | { |
| 2733 | dtm_cache_reset_counters (); |
| 2734 | #ifdef WDL_PROBE |
| 2735 | wdl_cache_reset_counters (); |
| 2736 | #endif |
| 2737 | eg_was_open_reset(); |
| 2738 | Drive.hits = 0; |
| 2739 | Drive.miss = 0; |
| 2740 | return; |
| 2741 | } |
| 2742 | |
| 2743 | static dtm_block_t * |
| 2744 | dtm_cache_pointblock (tbkey_t key, unsigned side, index_t idx) |
| 2745 | { |
| 2746 | index_t offset; |
| 2747 | index_t remainder; |
| 2748 | dtm_block_t * p; |
| 2749 | dtm_block_t * ret; |
| 2750 | |
| 2751 | if (!dtm_cache_is_on()) |
| 2752 | return NULL; |
| 2753 | |
| 2754 | split_index (dtm_cache.entries_per_block, idx, &offset, &remainder); |
| 2755 | |
| 2756 | ret = NULL; |
| 2757 | |
| 2758 | for (p = dtm_cache.top; p != NULL; p = p->prev) { |
| 2759 | |
| 2760 | dtm_cache.comparisons++; |
| 2761 | |
| 2762 | if (key == p->key && side == p->side && offset == p->offset) { |
| 2763 | ret = p; |
| 2764 | break; |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | FOLLOW_LU("point_to_dtm_block ok?",(ret!=NULL)) |
| 2769 | |
| 2770 | return ret; |
| 2771 | } |
| 2772 | |
| 2773 | /****************************************************************************\ |
| 2774 | | |
| 2775 | | |
| 2776 | | WRAPPERS for ENCODING/DECODING FUNCTIONS ZONE |
| 2777 | | |
| 2778 | | |
| 2779 | \****************************************************************************/ |
| 2780 | |
| 2781 | #include "gtb-dec.h" |
| 2782 | |
| 2783 | /* |
| 2784 | | |
| 2785 | | PRE LOAD CACHE AND DEPENDENT FUNCTIONS |
| 2786 | | |
| 2787 | \*--------------------------------------------------------------------------*/ |
| 2788 | |
| 2789 | struct ZIPINFO { |
| 2790 | index_t extraoffset; |
| 2791 | index_t totalblocks; |
| 2792 | index_t * blockindex; |
| 2793 | }; |
| 2794 | |
| 2795 | struct ZIPINFO Zipinfo[MAX_EGKEYS]; |
| 2796 | |
| 2797 | static index_t egtb_block_getnumber (tbkey_t key, unsigned side, index_t idx); |
| 2798 | static index_t egtb_block_getsize (tbkey_t key, index_t idx); |
| 2799 | static index_t egtb_block_getsize_zipped (tbkey_t key, index_t block ); |
| 2800 | static bool_t egtb_block_park (tbkey_t key, index_t block); |
| 2801 | static bool_t egtb_block_read (tbkey_t key, index_t len, unsigned char *buffer); |
| 2802 | static bool_t egtb_block_decode (tbkey_t key, index_t z, unsigned char *bz, index_t n, unsigned char *bp); |
| 2803 | static bool_t egtb_block_unpack (unsigned side, index_t n, const unsigned char *bp, dtm_t *out); |
| 2804 | static bool_t egtb_file_beready (tbkey_t key); |
| 2805 | static bool_t egtb_loadindexes (tbkey_t key); |
| 2806 | static index_t egtb_block_uncompressed_to_index (tbkey_t key, index_t b); |
| 2807 | static bool_t fread32 (FILE *f, /*@out@*/ unsigned long int *y); |
| 2808 | |
| 2809 | |
| 2810 | static unsigned int |
| 2811 | zipinfo_init (void) |
| 2812 | { |
| 2813 | int i, start, end; |
| 2814 | unsigned ret; |
| 2815 | bool_t ok, complet[8] = {0,0,0,0,0,0,0,0}; |
| 2816 | bool_t pa, partial[8] = {0,0,0,0,0,0,0,0}; |
| 2817 | unsigned int z; |
| 2818 | int x, j; |
| 2819 | |
| 2820 | /* reset all values */ |
| 2821 | for (i = 0; i < MAX_EGKEYS; i++) { |
| 2822 | Zipinfo[i].blockindex = NULL; |
| 2823 | Zipinfo[i].extraoffset = 0; |
| 2824 | Zipinfo[i].totalblocks = 0; |
| 2825 | } |
| 2826 | |
| 2827 | /* load all values */ |
| 2828 | start = 0; |
| 2829 | end = 5; |
| 2830 | x = 3; |
| 2831 | for (i = start, ok = TRUE, pa = FALSE; i < end; i++) { |
| 2832 | ok = NULL != fd_openit(i); |
| 2833 | pa = pa || ok; |
| 2834 | ok = ok && egtb_loadindexes (i); |
| 2835 | } |
| 2836 | complet[x] = ok; |
| 2837 | partial[x] = pa; |
| 2838 | |
| 2839 | start = 5; |
| 2840 | end = 35; |
| 2841 | x = 4; |
| 2842 | for (i = start, ok = TRUE, pa = FALSE; i < end; i++) { |
| 2843 | ok = NULL != fd_openit(i); |
| 2844 | pa = pa || ok; |
| 2845 | ok = ok && egtb_loadindexes (i); |
| 2846 | } |
| 2847 | complet[x] = ok; |
| 2848 | partial[x] = pa; |
| 2849 | |
| 2850 | start = 35; |
| 2851 | end = MAX_EGKEYS; |
| 2852 | x = 5; |
| 2853 | for (i = start, ok = TRUE, pa = FALSE; i < end; i++) { |
| 2854 | ok = NULL != fd_openit(i); |
| 2855 | pa = pa || ok; |
| 2856 | ok = ok && egtb_loadindexes (i); |
| 2857 | } |
| 2858 | complet[x] = ok; |
| 2859 | partial[x] = pa; |
| 2860 | |
| 2861 | |
| 2862 | for (j = 0, z = 0, x = 3; x < 8; x++) { |
| 2863 | if (partial[x]) z |= 1u << j; |
| 2864 | j++; |
| 2865 | if (complet[x]) z |= 1u << j; |
| 2866 | j++; |
| 2867 | } |
| 2868 | |
| 2869 | ret = z; |
| 2870 | |
| 2871 | return ret; |
| 2872 | } |
| 2873 | |
| 2874 | static void |
| 2875 | zipinfo_done (void) |
| 2876 | { |
| 2877 | int i; |
| 2878 | bool_t ok; |
| 2879 | for (i = 0, ok = TRUE; ok && i < MAX_EGKEYS; i++) { |
| 2880 | if (Zipinfo[i].blockindex != NULL) { |
| 2881 | free(Zipinfo[i].blockindex); |
| 2882 | Zipinfo[i].blockindex = NULL; |
| 2883 | Zipinfo[i].extraoffset = 0; |
| 2884 | Zipinfo[i].totalblocks = 0; |
| 2885 | } |
| 2886 | } |
| 2887 | return; |
| 2888 | } |
| 2889 | |
| 2890 | static size_t |
| 2891 | zipinfo_memory_allocated (void) |
| 2892 | { |
| 2893 | int i; |
| 2894 | index_t accum_blocks = 0; |
| 2895 | for (i = 0; i < MAX_EGKEYS; i++) { |
| 2896 | if (Zipinfo[i].blockindex != NULL) { |
| 2897 | accum_blocks += Zipinfo[i].totalblocks; |
| 2898 | } |
| 2899 | } |
| 2900 | return (size_t)accum_blocks * sizeof(index_t); |
| 2901 | } |
| 2902 | |
| 2903 | extern size_t |
| 2904 | tb_indexmemory (void) |
| 2905 | { |
| 2906 | return zipinfo_memory_allocated (); |
| 2907 | } |
| 2908 | |
| 2909 | static bool_t |
| 2910 | fread32 (FILE *f, /*@out@*/ unsigned long int *y) |
| 2911 | { |
| 2912 | enum SIZE {SZ = 4}; |
| 2913 | int i; |
| 2914 | unsigned long int x; |
| 2915 | unsigned char p[SZ]; |
| 2916 | bool_t ok; |
| 2917 | |
| 2918 | ok = (SZ == fread (p, sizeof(unsigned char), SZ, f)); |
| 2919 | |
| 2920 | if (ok) { |
| 2921 | for (x = 0, i = 0; i < SZ; i++) { |
| 2922 | x |= (unsigned long int)p[i] << (i*8); |
| 2923 | } |
| 2924 | *y = x; |
| 2925 | } |
| 2926 | return ok; |
| 2927 | } |
| 2928 | |
| 2929 | static bool_t |
| 2930 | egtb_loadindexes (tbkey_t key) |
| 2931 | { |
| 2932 | |
| 2933 | unsigned long int blocksize = 1; |
| 2934 | unsigned long int tailblocksize1 = 0; |
| 2935 | unsigned long int tailblocksize2 = 0; |
| 2936 | unsigned long int offset = 0; |
| 2937 | unsigned long int dummy; |
| 2938 | unsigned long int i; |
| 2939 | unsigned long int blocks; |
| 2940 | unsigned long int n_idx; |
| 2941 | unsigned long int idx = 0; /* to silence warning "may be used uninitialized..." */ |
| 2942 | index_t *p; |
| 2943 | |
| 2944 | bool_t ok; |
| 2945 | |
| 2946 | FILE *f; |
| 2947 | |
| 2948 | if (Uncompressed) { |
| 2949 | assert (decoding_scheme() == 0 && GTB_scheme == 0); |
| 2950 | return TRUE; /* no need to load indexes */ |
| 2951 | } |
| 2952 | if (Zipinfo[key].blockindex != NULL) |
| 2953 | return TRUE; /* indexes must have been loaded already */ |
| 2954 | |
| 2955 | if (NULL == (f = egkey[key].fd)) |
| 2956 | return FALSE; /* file was no open */ |
| 2957 | |
| 2958 | /* Get Reserved bytes, blocksize, offset */ |
| 2959 | ok = (0 == fseek (f, 0, SEEK_SET)) && |
| 2960 | fread32 (f, &dummy) && |
| 2961 | fread32 (f, &dummy) && |
| 2962 | fread32 (f, &blocksize) && |
| 2963 | fread32 (f, &dummy) && |
| 2964 | fread32 (f, &tailblocksize1) && |
| 2965 | fread32 (f, &dummy) && |
| 2966 | fread32 (f, &tailblocksize2) && |
| 2967 | fread32 (f, &dummy) && |
| 2968 | fread32 (f, &offset) && |
| 2969 | fread32 (f, &dummy); |
| 2970 | |
| 2971 | blocks = (offset - 40)/4 -1; |
| 2972 | n_idx = blocks + 1; |
| 2973 | |
| 2974 | p = NULL; |
| 2975 | |
| 2976 | ok = ok && NULL != (p = (index_t *)malloc (n_idx * sizeof(index_t))); |
| 2977 | |
| 2978 | /* Input of Indexes */ |
| 2979 | for (i = 0; ok && i < n_idx; i++) { |
| 2980 | ok = fread32 (f, &idx); /* idx will be used if ok is true, otherwise, it is discarded */ |
| 2981 | p[i] = (index_t)idx; /* reads a 32 bit int, and converts it to index_t */ assert (sizeof(index_t) >= 4); |
| 2982 | } |
| 2983 | |
| 2984 | if (ok) { |
| 2985 | Zipinfo[key].extraoffset = 0; |
| 2986 | assert (n_idx <= MAXINDEX_T); |
| 2987 | Zipinfo[key].totalblocks = (index_t) n_idx; |
| 2988 | Zipinfo[key].blockindex = p; |
| 2989 | } |
| 2990 | |
| 2991 | if (!ok && p != NULL) { |
| 2992 | free(p); |
| 2993 | } |
| 2994 | |
| 2995 | return ok; |
| 2996 | } |
| 2997 | |
| 2998 | static index_t |
| 2999 | egtb_block_uncompressed_to_index (tbkey_t key, index_t b) |
| 3000 | { |
| 3001 | index_t max; |
| 3002 | index_t blocks_per_side; |
| 3003 | index_t idx; |
| 3004 | |
| 3005 | max = egkey[key].maxindex; |
| 3006 | blocks_per_side = 1 + (max-1) / (index_t)dtm_cache.entries_per_block; |
| 3007 | |
| 3008 | if (b < blocks_per_side) { |
| 3009 | idx = 0; |
| 3010 | } else { |
| 3011 | b -= blocks_per_side; |
| 3012 | idx = max; |
| 3013 | } |
| 3014 | idx += b * (index_t)dtm_cache.entries_per_block; |
| 3015 | return idx; |
| 3016 | } |
| 3017 | |
| 3018 | |
| 3019 | static index_t |
| 3020 | egtb_block_getnumber (tbkey_t key, unsigned side, index_t idx) |
| 3021 | { |
| 3022 | index_t blocks_per_side; |
| 3023 | index_t block_in_side; |
| 3024 | index_t max = egkey[key].maxindex; |
| 3025 | |
| 3026 | blocks_per_side = 1 + (max-1) / (index_t)dtm_cache.entries_per_block; |
| 3027 | block_in_side = idx / (index_t)dtm_cache.entries_per_block; |
| 3028 | |
| 3029 | return (index_t)side * blocks_per_side + block_in_side; /* block */ |
| 3030 | } |
| 3031 | |
| 3032 | |
| 3033 | static index_t |
| 3034 | egtb_block_getsize (tbkey_t key, index_t idx) |
| 3035 | { |
| 3036 | index_t blocksz = (index_t) dtm_cache.entries_per_block; |
| 3037 | index_t maxindex = egkey[key].maxindex; |
| 3038 | index_t block, offset, x; |
| 3039 | |
| 3040 | assert (dtm_cache.entries_per_block <= MAXINDEX_T); |
| 3041 | assert (0 <= idx && idx < maxindex); |
| 3042 | assert (key < MAX_EGKEYS); |
| 3043 | |
| 3044 | block = idx / blocksz; |
| 3045 | offset = block * blocksz; |
| 3046 | |
| 3047 | /* |
| 3048 | | adjust block size in case that this is the last block |
| 3049 | | and is shorter than "blocksz" |
| 3050 | */ |
| 3051 | if ( (offset + blocksz) > maxindex) |
| 3052 | x = maxindex - offset; /* last block size */ |
| 3053 | else |
| 3054 | x = blocksz; /* size of a normal block */ |
| 3055 | |
| 3056 | return x; |
| 3057 | } |
| 3058 | |
| 3059 | static index_t |
| 3060 | egtb_block_getsize_zipped (tbkey_t key, index_t block ) |
| 3061 | { |
| 3062 | index_t i, j; |
| 3063 | assert (Zipinfo[key].blockindex != NULL); |
| 3064 | i = Zipinfo[key].blockindex[block]; |
| 3065 | j = Zipinfo[key].blockindex[block+1]; |
| 3066 | return j - i; |
| 3067 | } |
| 3068 | |
| 3069 | |
| 3070 | static bool_t |
| 3071 | egtb_file_beready (tbkey_t key) |
| 3072 | { |
| 3073 | bool_t success; |
| 3074 | assert (key < MAX_EGKEYS); |
| 3075 | success = (NULL != egkey[key].fd) || |
| 3076 | (NULL != fd_openit(key) && egtb_loadindexes (key)); |
| 3077 | return success; |
| 3078 | } |
| 3079 | |
| 3080 | |
| 3081 | static bool_t |
| 3082 | egtb_block_park (tbkey_t key, index_t block) |
| 3083 | { |
| 3084 | index_t i; |
| 3085 | long fseek_i; |
| 3086 | assert (egkey[key].fd != NULL); |
| 3087 | |
| 3088 | if (Uncompressed) { |
| 3089 | assert (decoding_scheme() == 0 && GTB_scheme == 0); |
| 3090 | i = egtb_block_uncompressed_to_index (key, block); |
| 3091 | } else { |
| 3092 | assert (Zipinfo[key].blockindex != NULL); |
| 3093 | i = Zipinfo[key].blockindex[block]; |
| 3094 | i += Zipinfo[key].extraoffset; |
| 3095 | } |
| 3096 | |
| 3097 | fseek_i = (long) i; |
| 3098 | assert (fseek_i >= 0); |
| 3099 | return 0 == fseek (egkey[key].fd, fseek_i, SEEK_SET); |
| 3100 | } |
| 3101 | |
| 3102 | |
| 3103 | static bool_t |
| 3104 | egtb_block_read (tbkey_t key, index_t len, unsigned char *buffer) |
| 3105 | { |
| 3106 | assert (egkey[key].fd != NULL); |
| 3107 | assert (sizeof(size_t) >= sizeof(len)); |
| 3108 | return ((size_t)len == fread (buffer, sizeof (unsigned char), (size_t)len, egkey[key].fd)); |
| 3109 | } |
| 3110 | |
| 3111 | tbkey_t TB_PROBE_indexing_dummy; |
| 3112 | |
| 3113 | static bool_t |
| 3114 | egtb_block_decode (tbkey_t key, index_t z, unsigned char *bz, index_t n, unsigned char *bp) |
| 3115 | /* bz:buffer zipped to bp:buffer packed */ |
| 3116 | { |
| 3117 | size_t zz = (size_t) z; |
| 3118 | size_t nn = (size_t) n; |
| 3119 | TB_PROBE_indexing_dummy = key; /* to silence compiler */ |
| 3120 | assert (sizeof(size_t) >= sizeof(n)); |
| 3121 | assert (sizeof(size_t) >= sizeof(z)); |
| 3122 | return decode (zz-1, bz+1, nn, bp); |
| 3123 | } |
| 3124 | |
| 3125 | static bool_t |
| 3126 | egtb_block_unpack (unsigned side, index_t n, const unsigned char *bp, dtm_t *out) |
| 3127 | /* bp:buffer packed to out:distance to mate buffer */ |
| 3128 | { |
| 3129 | index_t i; |
| 3130 | for (i = 0; i < n; i++) { |
| 3131 | *out++ = dtm_unpack (side, bp[i]); |
| 3132 | } |
| 3133 | return TRUE; |
| 3134 | } |
| 3135 | |
| 3136 | static bool_t |
| 3137 | preload_cache (tbkey_t key, unsigned side, index_t idx) |
| 3138 | /* output to the least used block of the cache */ |
| 3139 | { |
| 3140 | dtm_block_t *pblock; |
| 3141 | dtm_t *p; |
| 3142 | bool_t ok; |
| 3143 | |
| 3144 | FOLLOW_label("preload_cache starts") |
| 3145 | |
| 3146 | if (idx >= egkey[key].maxindex) { |
| 3147 | FOLLOW_LULU("Wrong index", __LINE__, idx) |
| 3148 | return FALSE; |
| 3149 | } |
| 3150 | |
| 3151 | /* find aged blocked in cache */ |
| 3152 | pblock = point_block_to_replace(); |
| 3153 | |
| 3154 | if (NULL == pblock) |
| 3155 | return FALSE; |
| 3156 | |
| 3157 | p = pblock->p_arr; |
| 3158 | |
| 3159 | if (Uncompressed) { |
| 3160 | |
| 3161 | index_t block = egtb_block_getnumber (key, side, idx); |
| 3162 | index_t n = egtb_block_getsize (key, idx); |
| 3163 | |
| 3164 | ok = egtb_file_beready (key) |
| 3165 | && egtb_block_park (key, block) |
| 3166 | && egtb_block_read (key, n, Buffer_packed) |
| 3167 | && egtb_block_unpack (side, n, Buffer_packed, p); |
| 3168 | |
| 3169 | FOLLOW_LULU("preload_cache", __LINE__, ok) |
| 3170 | |
| 3171 | assert (decoding_scheme() == 0 && GTB_scheme == 0); |
| 3172 | |
| 3173 | if (ok) { Bytes_read = Bytes_read + (uint64_t) n; } |
| 3174 | |
| 3175 | } else { |
| 3176 | |
| 3177 | index_t block = 0; |
| 3178 | index_t n = 0; |
| 3179 | index_t z = 0; |
| 3180 | |
| 3181 | ok = egtb_file_beready (key); |
| 3182 | |
| 3183 | FOLLOW_LULU("preload_cache", __LINE__, ok) |
| 3184 | |
| 3185 | if (ok) { |
| 3186 | block = egtb_block_getnumber (key, side, idx); |
| 3187 | n = egtb_block_getsize (key, idx); |
| 3188 | z = egtb_block_getsize_zipped (key, block); |
| 3189 | } |
| 3190 | |
| 3191 | ok = ok |
| 3192 | && egtb_block_park (key, block); |
| 3193 | FOLLOW_LULU("preload_cache", __LINE__, ok) |
| 3194 | |
| 3195 | ok = ok |
| 3196 | && egtb_block_read (key, z, Buffer_zipped); |
| 3197 | FOLLOW_LULU("preload_cache", __LINE__, ok) |
| 3198 | |
| 3199 | ok = ok |
| 3200 | && egtb_block_decode (key, z, Buffer_zipped, n, Buffer_packed); |
| 3201 | FOLLOW_LULU("preload_cache", __LINE__, ok) |
| 3202 | |
| 3203 | ok = ok |
| 3204 | && egtb_block_unpack (side, n, Buffer_packed, p); |
| 3205 | FOLLOW_LULU("preload_cache", __LINE__, ok) |
| 3206 | |
| 3207 | if (ok) { Bytes_read = Bytes_read + (uint64_t) z; } |
| 3208 | } |
| 3209 | |
| 3210 | if (ok) { |
| 3211 | |
| 3212 | index_t offset; |
| 3213 | index_t remainder; |
| 3214 | split_index (dtm_cache.entries_per_block, idx, &offset, &remainder); |
| 3215 | |
| 3216 | pblock->key = key; |
| 3217 | pblock->side = side; |
| 3218 | pblock->offset = offset; |
| 3219 | } else { |
| 3220 | /* make it unusable */ |
| 3221 | pblock->key = -1; |
| 3222 | pblock->side = gtbNOSIDE; |
| 3223 | pblock->offset = gtbNOINDEX; |
| 3224 | } |
| 3225 | |
| 3226 | FOLLOW_LU("preload_cache?", ok) |
| 3227 | |
| 3228 | return ok; |
| 3229 | } |
| 3230 | |
| 3231 | /****************************************************************************\ |
| 3232 | | |
| 3233 | | |
| 3234 | | MEMORY ALLOCATION ZONE |
| 3235 | | |
| 3236 | | |
| 3237 | \****************************************************************************/ |
| 3238 | |
| 3239 | |
| 3240 | mySHARED void |
| 3241 | egtb_freemem (int i) |
| 3242 | { |
| 3243 | if (egkey[i].status == STATUS_MALLOC) { |
| 3244 | assert (egkey[i].egt_w != NULL); |
| 3245 | assert (egkey[i].egt_b != NULL); |
| 3246 | free (egkey[i].egt_w); |
| 3247 | free (egkey[i].egt_b); |
| 3248 | egkey[i].egt_w = NULL; |
| 3249 | egkey[i].egt_b = NULL; |
| 3250 | } |
| 3251 | egkey[i].status = STATUS_ABSENT; |
| 3252 | } |
| 3253 | |
| 3254 | /***************************************************************************/ |
| 3255 | |
| 3256 | mySHARED bool_t |
| 3257 | get_dtm (tbkey_t key, unsigned side, index_t idx, dtm_t *out, bool_t probe_hard_flag) |
| 3258 | { |
| 3259 | bool_t found; |
| 3260 | |
| 3261 | if (probe_hard_flag) { |
| 3262 | dtm_cache.hard++; |
| 3263 | } else { |
| 3264 | dtm_cache.soft++; |
| 3265 | } |
| 3266 | |
| 3267 | if (get_dtm_from_cache (key, side, idx, out)) { |
| 3268 | dtm_cache.hits++; |
| 3269 | found = TRUE; |
| 3270 | } else if (probe_hard_flag) { |
| 3271 | dtm_cache.hardmisses++; |
| 3272 | found = preload_cache (key, side, idx) && |
| 3273 | get_dtm_from_cache (key, side, idx, out); |
| 3274 | |
| 3275 | if (found) { |
| 3276 | Drive.hits++; |
| 3277 | } else { |
| 3278 | Drive.miss++; |
| 3279 | } |
| 3280 | |
| 3281 | |
| 3282 | } else { |
| 3283 | dtm_cache.softmisses++; |
| 3284 | found = FALSE; |
| 3285 | } |
| 3286 | return found; |
| 3287 | } |
| 3288 | |
| 3289 | |
| 3290 | static bool_t |
| 3291 | get_dtm_from_cache (tbkey_t key, unsigned side, index_t idx, dtm_t *out) |
| 3292 | { |
| 3293 | index_t offset; |
| 3294 | index_t remainder; |
| 3295 | bool_t found; |
| 3296 | dtm_block_t *p; |
| 3297 | |
| 3298 | if (!dtm_cache_is_on()) |
| 3299 | return FALSE; |
| 3300 | |
| 3301 | split_index (dtm_cache.entries_per_block, idx, &offset, &remainder); |
| 3302 | |
| 3303 | found = NULL != (p = dtm_cache_pointblock (key, side, idx)); |
| 3304 | |
| 3305 | if (found) { |
| 3306 | *out = p->p_arr[remainder]; |
| 3307 | movetotop(p); |
| 3308 | } |
| 3309 | |
| 3310 | FOLLOW_LU("get_dtm_from_cache ok?",found) |
| 3311 | |
| 3312 | return found; |
| 3313 | } |
| 3314 | |
| 3315 | |
| 3316 | static void |
| 3317 | split_index (size_t entries_per_block, index_t i, index_t *o, index_t *r) |
| 3318 | { |
| 3319 | index_t n; |
| 3320 | n = i / (index_t) entries_per_block; |
| 3321 | *o = n * (index_t) entries_per_block; |
| 3322 | *r = i - *o; |
| 3323 | return; |
| 3324 | } |
| 3325 | |
| 3326 | |
| 3327 | static dtm_block_t * |
| 3328 | point_block_to_replace (void) |
| 3329 | { |
| 3330 | dtm_block_t *p, *t, *s; |
| 3331 | |
| 3332 | assert (0 == dtm_cache.n || dtm_cache.top != NULL); |
| 3333 | assert (0 == dtm_cache.n || dtm_cache.bot != NULL); |
| 3334 | assert (0 == dtm_cache.n || dtm_cache.bot->prev == NULL); |
| 3335 | assert (0 == dtm_cache.n || dtm_cache.top->next == NULL); |
| 3336 | |
| 3337 | /* no cache is being used */ |
| 3338 | if (dtm_cache.max_blocks == 0) |
| 3339 | return NULL; |
| 3340 | |
| 3341 | if (dtm_cache.n > 0 && -1 == dtm_cache.top->key) { |
| 3342 | |
| 3343 | /* top entry is unusable, should be the one to replace*/ |
| 3344 | p = dtm_cache.top; |
| 3345 | |
| 3346 | } else |
| 3347 | if (dtm_cache.n == 0) { |
| 3348 | |
| 3349 | assert (NULL != dtm_cache.entry); |
| 3350 | p = &dtm_cache.entry[dtm_cache.n++]; |
| 3351 | dtm_cache.top = p; |
| 3352 | dtm_cache.bot = p; |
| 3353 | |
| 3354 | assert (NULL != p); |
| 3355 | p->prev = NULL; |
| 3356 | p->next = NULL; |
| 3357 | |
| 3358 | } else |
| 3359 | if (dtm_cache.n < dtm_cache.max_blocks) { /* add */ |
| 3360 | |
| 3361 | assert (NULL != dtm_cache.entry); |
| 3362 | s = dtm_cache.top; |
| 3363 | p = &dtm_cache.entry[dtm_cache.n++]; |
| 3364 | dtm_cache.top = p; |
| 3365 | |
| 3366 | assert (NULL != p && NULL != s); |
| 3367 | s->next = p; |
| 3368 | p->prev = s; |
| 3369 | p->next = NULL; |
| 3370 | |
| 3371 | } else if (1 < dtm_cache.max_blocks) { /* replace*/ |
| 3372 | |
| 3373 | assert (NULL != dtm_cache.bot && NULL != dtm_cache.top); |
| 3374 | t = dtm_cache.bot; |
| 3375 | s = dtm_cache.top; |
| 3376 | |
| 3377 | dtm_cache.bot = t->next; |
| 3378 | dtm_cache.top = t; |
| 3379 | |
| 3380 | s->next = t; |
| 3381 | t->prev = s; |
| 3382 | |
| 3383 | assert (dtm_cache.top); |
| 3384 | dtm_cache.top->next = NULL; |
| 3385 | |
| 3386 | assert (dtm_cache.bot); |
| 3387 | dtm_cache.bot->prev = NULL; |
| 3388 | |
| 3389 | p = t; |
| 3390 | |
| 3391 | } else { |
| 3392 | |
| 3393 | assert (1 == dtm_cache.max_blocks); |
| 3394 | p = dtm_cache.top; |
| 3395 | assert (p == dtm_cache.bot && p == dtm_cache.entry); |
| 3396 | } |
| 3397 | |
| 3398 | /* make the information content unusable, it will be replaced */ |
| 3399 | p->key = -1; |
| 3400 | p->side = gtbNOSIDE; |
| 3401 | p->offset = gtbNOINDEX; |
| 3402 | |
| 3403 | return p; |
| 3404 | } |
| 3405 | |
| 3406 | static void |
| 3407 | movetotop (dtm_block_t *t) |
| 3408 | { |
| 3409 | dtm_block_t *s, *nx, *pv; |
| 3410 | |
| 3411 | assert (t != NULL); |
| 3412 | |
| 3413 | if (t->next == NULL) /* at the top already */ |
| 3414 | return; |
| 3415 | |
| 3416 | /* detach */ |
| 3417 | pv = t->prev; |
| 3418 | nx = t->next; |
| 3419 | |
| 3420 | if (pv == NULL) /* at the bottom */ |
| 3421 | dtm_cache.bot = nx; |
| 3422 | else |
| 3423 | pv->next = nx; |
| 3424 | |
| 3425 | if (nx == NULL) /* at the top */ |
| 3426 | dtm_cache.top = pv; |
| 3427 | else |
| 3428 | nx->prev = pv; |
| 3429 | |
| 3430 | /* relocate */ |
| 3431 | s = dtm_cache.top; |
| 3432 | assert (s != NULL); |
| 3433 | if (s == NULL) |
| 3434 | dtm_cache.bot = t; |
| 3435 | else |
| 3436 | s->next = t; |
| 3437 | |
| 3438 | t->next = NULL; |
| 3439 | t->prev = s; |
| 3440 | dtm_cache.top = t; |
| 3441 | |
| 3442 | return; |
| 3443 | } |
| 3444 | |
| 3445 | /****************************************************************************\ |
| 3446 | * |
| 3447 | * |
| 3448 | * INDEXING ZONE |
| 3449 | * |
| 3450 | * |
| 3451 | ****************************************************************************/ |
| 3452 | |
| 3453 | static void |
| 3454 | init_indexing (int verbosity) |
| 3455 | { |
| 3456 | index_t a,b,c,d,e,f; |
| 3457 | |
| 3458 | init_flipt (); |
| 3459 | |
| 3460 | a = init_kkidx () ; |
| 3461 | b = init_ppidx () ; |
| 3462 | c = init_aaidx () ; |
| 3463 | d = init_aaa () ; |
| 3464 | e = init_pp48_idx () ; |
| 3465 | f = init_ppp48_idx () ; |
| 3466 | |
| 3467 | if (verbosity) { |
| 3468 | printf ("\nGTB supporting tables, Initialization\n"); |
| 3469 | printf (" Max kk idx: %8d\n", (int)a ); |
| 3470 | printf (" Max pp idx: %8d\n", (int)b ); |
| 3471 | printf (" Max aa idx: %8d\n", (int)c ); |
| 3472 | printf (" Max aaa idx: %8d\n", (int)d ); |
| 3473 | printf (" Max pp48 idx: %8d\n", (int)e ); |
| 3474 | printf (" Max ppp48 idx: %8d\n", (int)f ); |
| 3475 | } |
| 3476 | |
| 3477 | if (!reach_was_initialized()) |
| 3478 | reach_init(); |
| 3479 | |
| 3480 | /* testing used only in development stage */ |
| 3481 | |
| 3482 | #ifdef _MSC_VER |
| 3483 | #pragma warning(disable:4127) |
| 3484 | #endif |
| 3485 | |
| 3486 | if (0) { |
| 3487 | list_index (); |
| 3488 | printf ("\nTEST indexing functions\n"); |
| 3489 | |
| 3490 | test_kaakb (); |
| 3491 | test_kaabk (); |
| 3492 | test_kaaak (); |
| 3493 | test_kabbk (); |
| 3494 | |
| 3495 | test_kapkb (); |
| 3496 | test_kabkp (); |
| 3497 | |
| 3498 | test_kppka (); |
| 3499 | |
| 3500 | test_kapkp (); |
| 3501 | test_kabpk(); |
| 3502 | test_kaapk (); |
| 3503 | |
| 3504 | test_kappk (); |
| 3505 | test_kaakp (); |
| 3506 | test_kppk (); |
| 3507 | test_kppkp (); |
| 3508 | test_kpppk (); |
| 3509 | } |
| 3510 | |
| 3511 | #ifdef _MSC_VER |
| 3512 | #pragma warning(default:4127) |
| 3513 | #endif |
| 3514 | |
| 3515 | return; |
| 3516 | } |
| 3517 | |
| 3518 | |
| 3519 | static index_t |
| 3520 | init_kkidx (void) |
| 3521 | /* modifies kkidx[][], wksq[], bksq[] */ |
| 3522 | { |
| 3523 | index_t idx; |
| 3524 | SQUARE x, y, i, j; |
| 3525 | |
| 3526 | /* default is noindex */ |
| 3527 | for (x = 0; x < 64; x++) { |
| 3528 | for (y = 0; y < 64; y++) { |
| 3529 | IDX_set_empty(kkidx [x][y]); |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | idx = 0; |
| 3534 | for (x = 0; x < 64; x++) { |
| 3535 | for (y = 0; y < 64; y++) { |
| 3536 | |
| 3537 | /* is x,y illegal? continue */ |
| 3538 | if (possible_attack (x, y, wK) || x == y) |
| 3539 | continue; |
| 3540 | |
| 3541 | /* normalize */ |
| 3542 | /*i <-- x; j <-- y */ |
| 3543 | norm_kkindex (x, y, &i, &j); |
| 3544 | |
| 3545 | if (IDX_is_empty(kkidx [i][j])) { /* still empty */ |
| 3546 | kkidx [i][j] = idx; |
| 3547 | kkidx [x][y] = idx; |
| 3548 | bksq [idx] = i; |
| 3549 | wksq [idx] = j; |
| 3550 | idx++; |
| 3551 | } |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | assert (idx == MAX_KKINDEX); |
| 3556 | |
| 3557 | return idx; |
| 3558 | } |
| 3559 | |
| 3560 | |
| 3561 | static index_t |
| 3562 | init_aaidx (void) |
| 3563 | /* modifies aabase[], aaidx[][] */ |
| 3564 | { |
| 3565 | index_t idx; |
| 3566 | SQUARE x, y; |
| 3567 | |
| 3568 | /* default is noindex */ |
| 3569 | for (x = 0; x < 64; x++) { |
| 3570 | for (y = 0; y < 64; y++) { |
| 3571 | IDX_set_empty(aaidx [x][y]); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | for (idx = 0; idx < MAX_AAINDEX; idx++) |
| 3576 | aabase [idx] = 0; |
| 3577 | |
| 3578 | idx = 0; |
| 3579 | for (x = 0; x < 64; x++) { |
| 3580 | for (y = x + 1; y < 64; y++) { |
| 3581 | |
| 3582 | assert (idx == (int)((y - x) + x * (127-x)/2 - 1) ); |
| 3583 | |
| 3584 | if (IDX_is_empty(aaidx [x][y])) { /* still empty */ |
| 3585 | aaidx [x] [y] = idx; |
| 3586 | aaidx [y] [x] = idx; |
| 3587 | aabase [idx] = (unsigned char) x; |
| 3588 | idx++; |
| 3589 | } else { |
| 3590 | assert (aaidx [x] [y] == idx); |
| 3591 | assert (aabase [idx] == x); |
| 3592 | } |
| 3593 | |
| 3594 | |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | assert (idx == MAX_AAINDEX); |
| 3599 | |
| 3600 | return idx; |
| 3601 | } |
| 3602 | |
| 3603 | |
| 3604 | static index_t |
| 3605 | init_ppidx (void) |
| 3606 | /* modifies ppidx[][], pp_hi24[], pp_lo48[] */ |
| 3607 | { |
| 3608 | index_t i, j; |
| 3609 | index_t idx = 0; |
| 3610 | SQUARE a, b; |
| 3611 | |
| 3612 | /* default is noindex */ |
| 3613 | for (i = 0; i < 24; i++) { |
| 3614 | for (j = 0; j < 48; j++) { |
| 3615 | IDX_set_empty(ppidx [i][j]); |
| 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | for (idx = 0; idx < MAX_PPINDEX; idx++) { |
| 3620 | IDX_set_empty(pp_hi24 [idx]); |
| 3621 | IDX_set_empty(pp_lo48 [idx]); |
| 3622 | } |
| 3623 | |
| 3624 | idx = 0; |
| 3625 | for (a = H7; a >= A2; a--) { |
| 3626 | |
| 3627 | if ((a & 07) < 4) /* square in the queen side */ |
| 3628 | continue; |
| 3629 | |
| 3630 | for (b = a - 1; b >= A2; b--) { |
| 3631 | |
| 3632 | SQUARE anchor, loosen; |
| 3633 | |
| 3634 | pp_putanchorfirst (a, b, &anchor, &loosen); |
| 3635 | |
| 3636 | if ((anchor & 07) > 3) { /* square in the king side */ |
| 3637 | anchor = flipWE(anchor); |
| 3638 | loosen = flipWE(loosen); |
| 3639 | } |
| 3640 | |
| 3641 | i = wsq_to_pidx24 (anchor); |
| 3642 | j = wsq_to_pidx48 (loosen); |
| 3643 | |
| 3644 | if (IDX_is_empty(ppidx [i] [j])) { |
| 3645 | |
| 3646 | ppidx [i] [j] = idx; |
| 3647 | assert (idx < MAX_PPINDEX); |
| 3648 | pp_hi24 [idx] = i; |
| 3649 | assert (i < 24); |
| 3650 | pp_lo48 [idx] = j; |
| 3651 | assert (j < 48); |
| 3652 | idx++; |
| 3653 | } |
| 3654 | |
| 3655 | } |
| 3656 | } |
| 3657 | assert (idx == MAX_PPINDEX); |
| 3658 | return idx; |
| 3659 | } |
| 3660 | |
| 3661 | static void |
| 3662 | init_flipt (void) |
| 3663 | { |
| 3664 | unsigned int i, j; |
| 3665 | for (i = 0; i < 64; i++) { |
| 3666 | for (j = 0; j < 64; j++) { |
| 3667 | flipt [i] [j] = flip_type (i, j); |
| 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | /*--- NORMALIZE -------*/ |
| 3673 | |
| 3674 | static void |
| 3675 | norm_kkindex (SQUARE x, SQUARE y, /*@out@*/ SQUARE *pi, /*@out@*/ SQUARE *pj) |
| 3676 | { |
| 3677 | unsigned int rowx, rowy, colx, coly; |
| 3678 | |
| 3679 | assert (x < 64); |
| 3680 | assert (y < 64); |
| 3681 | |
| 3682 | if (getcol(x) > 3) { |
| 3683 | x = flipWE (x); /* x = x ^ 07 */ |
| 3684 | y = flipWE (y); |
| 3685 | } |
| 3686 | if (getrow(x) > 3) { |
| 3687 | x = flipNS (x); /* x = x ^ 070 */ |
| 3688 | y = flipNS (y); |
| 3689 | } |
| 3690 | rowx = getrow(x); |
| 3691 | colx = getcol(x); |
| 3692 | if ( rowx > colx ) { |
| 3693 | x = flipNW_SE (x); /* x = ((x&7)<<3) | (x>>3) */ |
| 3694 | y = flipNW_SE (y); |
| 3695 | } |
| 3696 | rowy = getrow(y); |
| 3697 | coly = getcol(y); |
| 3698 | if ( rowx == colx && rowy > coly) { |
| 3699 | x = flipNW_SE (x); |
| 3700 | y = flipNW_SE (y); |
| 3701 | } |
| 3702 | |
| 3703 | *pi = x; |
| 3704 | *pj = y; |
| 3705 | } |
| 3706 | |
| 3707 | static unsigned int |
| 3708 | flip_type (SQUARE x, SQUARE y) |
| 3709 | { |
| 3710 | unsigned int rowx, rowy, colx, coly; |
| 3711 | unsigned int ret = 0; |
| 3712 | |
| 3713 | assert (x < 64); |
| 3714 | assert (y < 64); |
| 3715 | |
| 3716 | |
| 3717 | if (getcol(x) > 3) { |
| 3718 | x = flipWE (x); /* x = x ^ 07 */ |
| 3719 | y = flipWE (y); |
| 3720 | ret |= 1; |
| 3721 | } |
| 3722 | if (getrow(x) > 3) { |
| 3723 | x = flipNS (x); /* x = x ^ 070 */ |
| 3724 | y = flipNS (y); |
| 3725 | ret |= 2; |
| 3726 | } |
| 3727 | rowx = getrow(x); |
| 3728 | colx = getcol(x); |
| 3729 | if ( rowx > colx ) { |
| 3730 | x = flipNW_SE (x); /* x = ((x&7)<<3) | (x>>3) */ |
| 3731 | y = flipNW_SE (y); |
| 3732 | ret |= 4; |
| 3733 | } |
| 3734 | rowy = getrow(y); |
| 3735 | coly = getcol(y); |
| 3736 | if ( rowx == colx && rowy > coly) { |
| 3737 | x = flipNW_SE (x); |
| 3738 | y = flipNW_SE (y); |
| 3739 | ret |= 4; |
| 3740 | } |
| 3741 | return ret; |
| 3742 | } |
| 3743 | |
| 3744 | |
| 3745 | static void |
| 3746 | pp_putanchorfirst (SQUARE a, SQUARE b, /*@out@*/ SQUARE *out_anchor, /*@out@*/ SQUARE *out_loosen) |
| 3747 | { |
| 3748 | unsigned int anchor, loosen; |
| 3749 | |
| 3750 | unsigned int row_b, row_a; |
| 3751 | row_b = b & 070; |
| 3752 | row_a = a & 070; |
| 3753 | |
| 3754 | /* default */ |
| 3755 | anchor = a; |
| 3756 | loosen = b; |
| 3757 | if (row_b > row_a) { |
| 3758 | anchor = b; |
| 3759 | loosen = a; |
| 3760 | } |
| 3761 | else |
| 3762 | if (row_b == row_a) { |
| 3763 | unsigned int x, col, inv, hi_a, hi_b; |
| 3764 | x = a; |
| 3765 | col = x & 07; |
| 3766 | inv = col ^ 07; |
| 3767 | x = (1u<<col) | (1u<<inv); |
| 3768 | x &= (x-1); |
| 3769 | hi_a = x; |
| 3770 | |
| 3771 | x = b; |
| 3772 | col = x & 07; |
| 3773 | inv = col ^ 07; |
| 3774 | x = (1u<<col) | (1u<<inv); |
| 3775 | x &= (x-1); |
| 3776 | hi_b = x; |
| 3777 | |
| 3778 | if (hi_b > hi_a) { |
| 3779 | anchor = b; |
| 3780 | loosen = a; |
| 3781 | } |
| 3782 | |
| 3783 | if (hi_b < hi_a) { |
| 3784 | anchor = a; |
| 3785 | loosen = b; |
| 3786 | } |
| 3787 | |
| 3788 | if (hi_b == hi_a) { |
| 3789 | if (a < b) { |
| 3790 | anchor = a; |
| 3791 | loosen = b; |
| 3792 | } else { |
| 3793 | anchor = b; |
| 3794 | loosen = a; |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | *out_anchor = anchor; |
| 3800 | *out_loosen = loosen; |
| 3801 | return; |
| 3802 | } |
| 3803 | |
| 3804 | |
| 3805 | static index_t |
| 3806 | wsq_to_pidx24 (SQUARE pawn) |
| 3807 | { |
| 3808 | unsigned int idx24; |
| 3809 | SQUARE sq = pawn; |
| 3810 | |
| 3811 | /* input can be only queen side, pawn valid */ |
| 3812 | assert (A2 <= pawn && pawn < A8); |
| 3813 | assert ((pawn & 07) < 4); |
| 3814 | |
| 3815 | sq ^= 070; /* flipNS*/ |
| 3816 | sq -= 8; /* down one row*/ |
| 3817 | idx24 = (sq+(sq&3)) >> 1; |
| 3818 | assert (idx24 < 24); |
| 3819 | return (index_t) idx24; |
| 3820 | } |
| 3821 | |
| 3822 | static index_t |
| 3823 | wsq_to_pidx48 (SQUARE pawn) |
| 3824 | { |
| 3825 | unsigned int idx48; |
| 3826 | SQUARE sq = pawn; |
| 3827 | |
| 3828 | /* input can be both queen or king side, pawn valid square */ |
| 3829 | assert (A2 <= pawn && pawn < A8); |
| 3830 | |
| 3831 | sq ^= 070; /* flipNS*/ |
| 3832 | sq -= 8; /* down one row*/ |
| 3833 | idx48 = sq; |
| 3834 | assert (idx48 < 48); |
| 3835 | return (index_t)idx48; |
| 3836 | } |
| 3837 | |
| 3838 | static SQUARE |
| 3839 | pidx24_to_wsq (index_t a) |
| 3840 | { |
| 3841 | enum {B11100 = 7u << 2}; |
| 3842 | unsigned int x = (unsigned int) a; /* x is pslice */ |
| 3843 | assert (a < 24); |
| 3844 | |
| 3845 | x += x & B11100; /* get upper part and double it */ |
| 3846 | x += 8; /* add extra row */ |
| 3847 | x ^= 070; /* flip NS */ |
| 3848 | return (SQUARE) x; |
| 3849 | } |
| 3850 | |
| 3851 | static SQUARE |
| 3852 | pidx48_to_wsq (index_t a) |
| 3853 | { |
| 3854 | unsigned int x; |
| 3855 | assert (a < 48); |
| 3856 | /* x is pslice */ |
| 3857 | x = (unsigned int)a; |
| 3858 | x += 8; /* add extra row */ |
| 3859 | x ^= 070; /* flip NS */ |
| 3860 | return x; |
| 3861 | } |
| 3862 | |
| 3863 | |
| 3864 | static void |
| 3865 | kxk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 3866 | { |
| 3867 | enum {BLOCK_A = 64}; |
| 3868 | |
| 3869 | index_t a = i / BLOCK_A; |
| 3870 | index_t b = i - a * BLOCK_A; |
| 3871 | |
| 3872 | pw[0] = wksq [a]; |
| 3873 | pb[0] = bksq [a]; |
| 3874 | pw[1] = (SQUARE) b; |
| 3875 | pw[2] = NOSQUARE; |
| 3876 | pb[1] = NOSQUARE; |
| 3877 | |
| 3878 | assert (kxk_pctoindex (pw, pb, &a) && a == i); |
| 3879 | |
| 3880 | return; |
| 3881 | } |
| 3882 | |
| 3883 | static bool_t |
| 3884 | kxk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 3885 | { |
| 3886 | enum {BLOCK_A = 64}; |
| 3887 | SQUARE *p; |
| 3888 | SQUARE ws[32], bs[32]; |
| 3889 | index_t ki; |
| 3890 | int i; |
| 3891 | |
| 3892 | unsigned int ft; |
| 3893 | |
| 3894 | ft = flip_type (inp_pb[0],inp_pw[0]); |
| 3895 | |
| 3896 | assert (ft < 8); |
| 3897 | |
| 3898 | |
| 3899 | for (i = 0; inp_pw[i] != NOSQUARE; i++) { |
| 3900 | ws[i] = inp_pw[i]; |
| 3901 | } |
| 3902 | ws[i] = NOSQUARE; |
| 3903 | for (i = 0; inp_pb[i] != NOSQUARE; i++) { |
| 3904 | bs[i] = inp_pb[i]; |
| 3905 | } |
| 3906 | bs[i] = NOSQUARE; |
| 3907 | |
| 3908 | if ((ft & 1) != 0) { |
| 3909 | for (p = ws; *p != NOSQUARE; p++) |
| 3910 | *p = flipWE (*p); |
| 3911 | for (p = bs; *p != NOSQUARE; p++) |
| 3912 | *p = flipWE (*p); |
| 3913 | } |
| 3914 | |
| 3915 | if ((ft & 2) != 0) { |
| 3916 | for (p = ws; *p != NOSQUARE; p++) |
| 3917 | *p = flipNS (*p); |
| 3918 | for (p = bs; *p != NOSQUARE; p++) |
| 3919 | *p = flipNS (*p); |
| 3920 | } |
| 3921 | |
| 3922 | if ((ft & 4) != 0) { |
| 3923 | for (p = ws; *p != NOSQUARE; p++) |
| 3924 | *p = flipNW_SE (*p); |
| 3925 | for (p = bs; *p != NOSQUARE; p++) |
| 3926 | *p = flipNW_SE (*p); |
| 3927 | } |
| 3928 | |
| 3929 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 3930 | |
| 3931 | if (IDX_is_empty(ki)) { |
| 3932 | *out = NOINDEX; |
| 3933 | return FALSE; |
| 3934 | } |
| 3935 | *out = ki * BLOCK_A + (index_t) ws[1]; |
| 3936 | return TRUE; |
| 3937 | |
| 3938 | } |
| 3939 | |
| 3940 | |
| 3941 | static void |
| 3942 | kabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 3943 | { |
| 3944 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 3945 | index_t a, b, c, r; |
| 3946 | |
| 3947 | r = i; |
| 3948 | a = r / BLOCK_A; |
| 3949 | r -= a * BLOCK_A; |
| 3950 | b = r / BLOCK_B; |
| 3951 | r -= b * BLOCK_B; |
| 3952 | c = r; |
| 3953 | |
| 3954 | pw[0] = wksq [a]; |
| 3955 | pb[0] = bksq [a]; |
| 3956 | |
| 3957 | pw[1] = (SQUARE) b; |
| 3958 | pw[2] = (SQUARE) c; |
| 3959 | pw[3] = NOSQUARE; |
| 3960 | |
| 3961 | pb[1] = NOSQUARE; |
| 3962 | |
| 3963 | assert (kabk_pctoindex (pw, pb, &a) && a == i); |
| 3964 | |
| 3965 | return; |
| 3966 | } |
| 3967 | |
| 3968 | |
| 3969 | static bool_t |
| 3970 | kabk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 3971 | { |
| 3972 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 3973 | SQUARE *p; |
| 3974 | SQUARE ws[32], bs[32]; |
| 3975 | index_t ki; |
| 3976 | int i; |
| 3977 | |
| 3978 | unsigned int ft; |
| 3979 | |
| 3980 | ft = flip_type (inp_pb[0],inp_pw[0]); |
| 3981 | |
| 3982 | assert (ft < 8); |
| 3983 | |
| 3984 | for (i = 0; inp_pw[i] != NOSQUARE; i++) { |
| 3985 | ws[i] = inp_pw[i]; |
| 3986 | } |
| 3987 | ws[i] = NOSQUARE; |
| 3988 | for (i = 0; inp_pb[i] != NOSQUARE; i++) { |
| 3989 | bs[i] = inp_pb[i]; |
| 3990 | } |
| 3991 | bs[i] = NOSQUARE; |
| 3992 | |
| 3993 | if ((ft & 1) != 0) { |
| 3994 | for (p = ws; *p != NOSQUARE; p++) |
| 3995 | *p = flipWE (*p); |
| 3996 | for (p = bs; *p != NOSQUARE; p++) |
| 3997 | *p = flipWE (*p); |
| 3998 | } |
| 3999 | |
| 4000 | if ((ft & 2) != 0) { |
| 4001 | for (p = ws; *p != NOSQUARE; p++) |
| 4002 | *p = flipNS (*p); |
| 4003 | for (p = bs; *p != NOSQUARE; p++) |
| 4004 | *p = flipNS (*p); |
| 4005 | } |
| 4006 | |
| 4007 | if ((ft & 4) != 0) { |
| 4008 | for (p = ws; *p != NOSQUARE; p++) |
| 4009 | *p = flipNW_SE (*p); |
| 4010 | for (p = bs; *p != NOSQUARE; p++) |
| 4011 | *p = flipNW_SE (*p); |
| 4012 | } |
| 4013 | |
| 4014 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4015 | |
| 4016 | if (IDX_is_empty(ki)) { |
| 4017 | *out = NOINDEX; |
| 4018 | return FALSE; |
| 4019 | } |
| 4020 | *out = ki * BLOCK_A + (index_t)ws[1] * BLOCK_B + (index_t)ws[2]; |
| 4021 | return TRUE; |
| 4022 | |
| 4023 | } |
| 4024 | |
| 4025 | |
| 4026 | static void |
| 4027 | kabkc_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4028 | { |
| 4029 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 4030 | index_t a, b, c, d, r; |
| 4031 | |
| 4032 | r = i; |
| 4033 | a = r / BLOCK_A; |
| 4034 | r -= a * BLOCK_A; |
| 4035 | b = r / BLOCK_B; |
| 4036 | r -= b * BLOCK_B; |
| 4037 | c = r / BLOCK_C; |
| 4038 | r -= c * BLOCK_C; |
| 4039 | d = r; |
| 4040 | |
| 4041 | pw[0] = wksq [a]; |
| 4042 | pb[0] = bksq [a]; |
| 4043 | |
| 4044 | pw[1] = (SQUARE) b; |
| 4045 | pw[2] = (SQUARE) c; |
| 4046 | pw[3] = NOSQUARE; |
| 4047 | |
| 4048 | pb[1] = (SQUARE) d; |
| 4049 | pb[2] = NOSQUARE; |
| 4050 | |
| 4051 | assert (kabkc_pctoindex (pw, pb, &a) && a == i); |
| 4052 | |
| 4053 | return; |
| 4054 | } |
| 4055 | |
| 4056 | |
| 4057 | static bool_t |
| 4058 | kabkc_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 4059 | { |
| 4060 | enum {N_WHITE = 3, N_BLACK = 2}; |
| 4061 | |
| 4062 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 4063 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 4064 | index_t ki; |
| 4065 | int i; |
| 4066 | unsigned int ft; |
| 4067 | |
| 4068 | #if 0 |
| 4069 | ft = flip_type (inp_pb[0], inp_pw[0]); |
| 4070 | #else |
| 4071 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4072 | #endif |
| 4073 | |
| 4074 | assert (ft < 8); |
| 4075 | |
| 4076 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; ws[N_WHITE] = NOSQUARE; |
| 4077 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; bs[N_BLACK] = NOSQUARE; |
| 4078 | |
| 4079 | if ((ft & WE_FLAG) != 0) { |
| 4080 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 4081 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 4082 | } |
| 4083 | |
| 4084 | if ((ft & NS_FLAG) != 0) { |
| 4085 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 4086 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 4087 | } |
| 4088 | |
| 4089 | if ((ft & NW_SE_FLAG) != 0) { |
| 4090 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 4091 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 4092 | } |
| 4093 | |
| 4094 | |
| 4095 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4096 | |
| 4097 | if (IDX_is_empty(ki)) { |
| 4098 | *out = NOINDEX; |
| 4099 | return FALSE; |
| 4100 | } |
| 4101 | *out = ki * BLOCK_A + (index_t)ws[1] * BLOCK_B + (index_t)ws[2] * BLOCK_C + (index_t)bs[1]; |
| 4102 | return TRUE; |
| 4103 | |
| 4104 | } |
| 4105 | |
| 4106 | /* ABC/ ***/ |
| 4107 | |
| 4108 | extern void |
| 4109 | kabck_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4110 | { |
| 4111 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 4112 | index_t a, b, c, d, r; |
| 4113 | |
| 4114 | r = i; |
| 4115 | a = r / BLOCK_A; |
| 4116 | r -= a * BLOCK_A; |
| 4117 | b = r / BLOCK_B; |
| 4118 | r -= b * BLOCK_B; |
| 4119 | c = r / BLOCK_C; |
| 4120 | r -= c * BLOCK_C; |
| 4121 | d = r; |
| 4122 | |
| 4123 | pw[0] = wksq [a]; |
| 4124 | pb[0] = bksq [a]; |
| 4125 | |
| 4126 | pw[1] = (SQUARE) b; |
| 4127 | pw[2] = (SQUARE) c; |
| 4128 | pw[3] = (SQUARE) d; |
| 4129 | pw[4] = NOSQUARE; |
| 4130 | |
| 4131 | pb[1] = NOSQUARE; |
| 4132 | |
| 4133 | assert (kabck_pctoindex (pw, pb, &a) && a == i); |
| 4134 | |
| 4135 | return; |
| 4136 | } |
| 4137 | |
| 4138 | |
| 4139 | extern bool_t |
| 4140 | kabck_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 4141 | { |
| 4142 | enum {N_WHITE = 4, N_BLACK = 1}; |
| 4143 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 4144 | |
| 4145 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 4146 | index_t ki; |
| 4147 | int i; |
| 4148 | unsigned int ft; |
| 4149 | |
| 4150 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4151 | |
| 4152 | assert (ft < 8); |
| 4153 | |
| 4154 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; ws[N_WHITE] = NOSQUARE; |
| 4155 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; bs[N_BLACK] = NOSQUARE; |
| 4156 | |
| 4157 | if ((ft & WE_FLAG) != 0) { |
| 4158 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 4159 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 4160 | } |
| 4161 | |
| 4162 | if ((ft & NS_FLAG) != 0) { |
| 4163 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 4164 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 4165 | } |
| 4166 | |
| 4167 | if ((ft & NW_SE_FLAG) != 0) { |
| 4168 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 4169 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 4170 | } |
| 4171 | |
| 4172 | |
| 4173 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4174 | |
| 4175 | if (IDX_is_empty(ki)) { |
| 4176 | *out = NOINDEX; |
| 4177 | return FALSE; |
| 4178 | } |
| 4179 | *out = ki * BLOCK_A + (index_t)ws[1] * BLOCK_B + (index_t)ws[2] * BLOCK_C + (index_t)ws[3]; |
| 4180 | return TRUE; |
| 4181 | |
| 4182 | } |
| 4183 | |
| 4184 | |
| 4185 | static void |
| 4186 | kakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4187 | { |
| 4188 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 4189 | index_t a, b, c, r; |
| 4190 | |
| 4191 | r = i; |
| 4192 | a = r / BLOCK_A; |
| 4193 | r -= a * BLOCK_A; |
| 4194 | b = r / BLOCK_B; |
| 4195 | r -= b * BLOCK_B; |
| 4196 | c = r; |
| 4197 | |
| 4198 | pw[0] = wksq [a]; |
| 4199 | pb[0] = bksq [a]; |
| 4200 | |
| 4201 | pw[1] = (SQUARE) b; |
| 4202 | pw[2] = NOSQUARE; |
| 4203 | |
| 4204 | pb[1] = (SQUARE) c; |
| 4205 | pb[2] = NOSQUARE; |
| 4206 | |
| 4207 | assert (kakb_pctoindex (pw, pb, &a) && a == i); |
| 4208 | |
| 4209 | return; |
| 4210 | } |
| 4211 | |
| 4212 | |
| 4213 | static bool_t |
| 4214 | kakb_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 4215 | { |
| 4216 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 4217 | SQUARE ws[32], bs[32]; |
| 4218 | index_t ki; |
| 4219 | unsigned int ft; |
| 4220 | |
| 4221 | #if 0 |
| 4222 | ft = flip_type (inp_pb[0], inp_pw[0]); |
| 4223 | #else |
| 4224 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4225 | #endif |
| 4226 | |
| 4227 | assert (ft < 8); |
| 4228 | |
| 4229 | ws[0] = inp_pw[0]; |
| 4230 | ws[1] = inp_pw[1]; |
| 4231 | ws[2] = NOSQUARE; |
| 4232 | |
| 4233 | bs[0] = inp_pb[0]; |
| 4234 | bs[1] = inp_pb[1]; |
| 4235 | bs[2] = NOSQUARE; |
| 4236 | |
| 4237 | if ((ft & 1) != 0) { |
| 4238 | ws[0] = flipWE (ws[0]); |
| 4239 | ws[1] = flipWE (ws[1]); |
| 4240 | bs[0] = flipWE (bs[0]); |
| 4241 | bs[1] = flipWE (bs[1]); |
| 4242 | } |
| 4243 | |
| 4244 | if ((ft & 2) != 0) { |
| 4245 | ws[0] = flipNS (ws[0]); |
| 4246 | ws[1] = flipNS (ws[1]); |
| 4247 | bs[0] = flipNS (bs[0]); |
| 4248 | bs[1] = flipNS (bs[1]); |
| 4249 | } |
| 4250 | |
| 4251 | if ((ft & 4) != 0) { |
| 4252 | ws[0] = flipNW_SE (ws[0]); |
| 4253 | ws[1] = flipNW_SE (ws[1]); |
| 4254 | bs[0] = flipNW_SE (bs[0]); |
| 4255 | bs[1] = flipNW_SE (bs[1]); |
| 4256 | } |
| 4257 | |
| 4258 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4259 | |
| 4260 | if (IDX_is_empty(ki)) { |
| 4261 | *out = NOINDEX; |
| 4262 | return FALSE; |
| 4263 | } |
| 4264 | *out = ki * BLOCK_A + (index_t)ws[1] * BLOCK_B + (index_t)bs[1]; |
| 4265 | return TRUE; |
| 4266 | |
| 4267 | } |
| 4268 | |
| 4269 | /********************** KAAKB *************************************/ |
| 4270 | |
| 4271 | static bool_t test_kaakb (void); |
| 4272 | static bool_t kaakb_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 4273 | static void kaakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 4274 | |
| 4275 | static bool_t |
| 4276 | test_kaakb (void) |
| 4277 | { |
| 4278 | |
| 4279 | enum {MAXPC = 16+1}; |
| 4280 | char str[] = "kaakb"; |
| 4281 | SQUARE a, b, c, d, e; |
| 4282 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 4283 | SQUARE px[MAXPC], py[MAXPC]; |
| 4284 | |
| 4285 | index_t i, j; |
| 4286 | bool_t err = FALSE; |
| 4287 | |
| 4288 | printf ("%8s ", str); |
| 4289 | |
| 4290 | for (a = 0; a < 64; a++) { |
| 4291 | for (b = 0; b < 64; b++) { |
| 4292 | for (c = 0; c < 64; c++) { |
| 4293 | for (d = 0; d < 64; d++) { |
| 4294 | for (e = 0; e < 64; e++) { |
| 4295 | |
| 4296 | pw[0] = a; |
| 4297 | pw[1] = b; |
| 4298 | pw[2] = c; |
| 4299 | pw[3] = NOSQUARE; |
| 4300 | |
| 4301 | pb[0] = d; |
| 4302 | pb[1] = e; |
| 4303 | pb[2] = NOSQUARE; |
| 4304 | |
| 4305 | if (kaakb_pctoindex (pw, pb, &i)) { |
| 4306 | kaakb_indextopc (i, px, py); |
| 4307 | kaakb_pctoindex (px, py, &j); |
| 4308 | if (i != j) { |
| 4309 | err = TRUE; |
| 4310 | } |
| 4311 | assert (i == j); |
| 4312 | } |
| 4313 | |
| 4314 | } |
| 4315 | } |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | if ((a&1)==0) { |
| 4320 | printf("."); |
| 4321 | fflush(stdout); |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | if (err) |
| 4326 | printf ("> %s NOT passed\n", str); |
| 4327 | else |
| 4328 | printf ("> %s PASSED\n", str); |
| 4329 | return !err; |
| 4330 | } |
| 4331 | |
| 4332 | static void |
| 4333 | kaakb_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4334 | { |
| 4335 | enum { |
| 4336 | BLOCK_B = 64, |
| 4337 | BLOCK_A = BLOCK_B * MAX_AAINDEX |
| 4338 | }; |
| 4339 | index_t a, b, c, r, x, y; |
| 4340 | |
| 4341 | r = i; |
| 4342 | a = r / BLOCK_A; |
| 4343 | r -= a * BLOCK_A; |
| 4344 | |
| 4345 | b = r / BLOCK_B; |
| 4346 | r -= b * BLOCK_B; |
| 4347 | |
| 4348 | c = r; |
| 4349 | |
| 4350 | assert (i == (a * BLOCK_A + b * BLOCK_B + c)); |
| 4351 | |
| 4352 | pw[0] = wksq [a]; |
| 4353 | pb[0] = bksq [a]; |
| 4354 | |
| 4355 | x = aabase [b]; |
| 4356 | y = (b + 1) + x - (x * (127-x)/2); |
| 4357 | |
| 4358 | pw[1] = (SQUARE) x; |
| 4359 | pw[2] = (SQUARE) y; |
| 4360 | pw[3] = NOSQUARE; |
| 4361 | |
| 4362 | pb[1] = (SQUARE) c; |
| 4363 | pb[2] = NOSQUARE; |
| 4364 | |
| 4365 | assert (kaakb_pctoindex (pw, pb, &a) && a == i); |
| 4366 | |
| 4367 | return; |
| 4368 | } |
| 4369 | |
| 4370 | static bool_t |
| 4371 | kaakb_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out) |
| 4372 | { |
| 4373 | enum {N_WHITE = 3, N_BLACK = 2}; |
| 4374 | enum { |
| 4375 | BLOCK_B = 64, |
| 4376 | BLOCK_A = BLOCK_B * MAX_AAINDEX |
| 4377 | }; |
| 4378 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 4379 | index_t ki, ai; |
| 4380 | unsigned int ft; |
| 4381 | int i; |
| 4382 | |
| 4383 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4384 | |
| 4385 | assert (ft < 8); |
| 4386 | |
| 4387 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; ws[N_WHITE] = NOSQUARE; |
| 4388 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; bs[N_BLACK] = NOSQUARE; |
| 4389 | |
| 4390 | if ((ft & WE_FLAG) != 0) { |
| 4391 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 4392 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 4393 | } |
| 4394 | |
| 4395 | if ((ft & NS_FLAG) != 0) { |
| 4396 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 4397 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 4398 | } |
| 4399 | |
| 4400 | if ((ft & NW_SE_FLAG) != 0) { |
| 4401 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 4402 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 4403 | } |
| 4404 | |
| 4405 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4406 | ai = aaidx [ws[1]] [ws[2]]; |
| 4407 | |
| 4408 | if (IDX_is_empty(ki) || IDX_is_empty(ai)) { |
| 4409 | *out = NOINDEX; |
| 4410 | return FALSE; |
| 4411 | } |
| 4412 | *out = ki * BLOCK_A + ai * BLOCK_B + (index_t)bs[1]; |
| 4413 | return TRUE; |
| 4414 | } |
| 4415 | |
| 4416 | /****************** End KAAKB *************************************/ |
| 4417 | |
| 4418 | /********************** KAAB/K ************************************/ |
| 4419 | |
| 4420 | static bool_t test_kaabk (void); |
| 4421 | static bool_t kaabk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 4422 | static void kaabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 4423 | |
| 4424 | static bool_t |
| 4425 | test_kaabk (void) |
| 4426 | { |
| 4427 | |
| 4428 | enum {MAXPC = 16+1}; |
| 4429 | char str[] = "kaabk"; |
| 4430 | SQUARE a, b, c, d, e; |
| 4431 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 4432 | SQUARE px[MAXPC], py[MAXPC]; |
| 4433 | |
| 4434 | index_t i, j; |
| 4435 | bool_t err = FALSE; |
| 4436 | |
| 4437 | printf ("%8s ", str); |
| 4438 | |
| 4439 | for (a = 0; a < 64; a++) { |
| 4440 | for (b = 0; b < 64; b++) { |
| 4441 | for (c = 0; c < 64; c++) { |
| 4442 | for (d = 0; d < 64; d++) { |
| 4443 | for (e = 0; e < 64; e++) { |
| 4444 | |
| 4445 | pw[0] = a; |
| 4446 | pw[1] = b; |
| 4447 | pw[2] = c; |
| 4448 | pw[3] = d; |
| 4449 | pw[4] = NOSQUARE; |
| 4450 | |
| 4451 | pb[0] = e; |
| 4452 | pb[1] = NOSQUARE; |
| 4453 | |
| 4454 | if (kaabk_pctoindex (pw, pb, &i)) { |
| 4455 | kaabk_indextopc (i, px, py); |
| 4456 | kaabk_pctoindex (px, py, &j); |
| 4457 | if (i != j) { |
| 4458 | err = TRUE; |
| 4459 | } |
| 4460 | assert (i == j); |
| 4461 | } |
| 4462 | |
| 4463 | } |
| 4464 | } |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | if ((a&1)==0) { |
| 4469 | printf("."); |
| 4470 | fflush(stdout); |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | if (err) |
| 4475 | printf ("> %s NOT passed\n", str); |
| 4476 | else |
| 4477 | printf ("> %s PASSED\n", str); |
| 4478 | return !err; |
| 4479 | } |
| 4480 | |
| 4481 | static void |
| 4482 | kaabk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4483 | { |
| 4484 | enum { |
| 4485 | BLOCK_B = 64, |
| 4486 | BLOCK_A = BLOCK_B * MAX_AAINDEX |
| 4487 | }; |
| 4488 | index_t a, b, c, r, x, y; |
| 4489 | |
| 4490 | r = i; |
| 4491 | a = r / BLOCK_A; |
| 4492 | r -= a * BLOCK_A; |
| 4493 | |
| 4494 | b = r / BLOCK_B; |
| 4495 | r -= b * BLOCK_B; |
| 4496 | |
| 4497 | c = r; |
| 4498 | |
| 4499 | assert (i == (a * BLOCK_A + b * BLOCK_B + c)); |
| 4500 | |
| 4501 | pw[0] = wksq [a]; |
| 4502 | pb[0] = bksq [a]; |
| 4503 | |
| 4504 | x = aabase [b]; |
| 4505 | y = (b + 1) + x - (x * (127-x)/2); |
| 4506 | |
| 4507 | pw[1] = (SQUARE) x; |
| 4508 | pw[2] = (SQUARE) y; |
| 4509 | pw[3] = (SQUARE) c; |
| 4510 | pw[4] = NOSQUARE; |
| 4511 | |
| 4512 | pb[1] = NOSQUARE; |
| 4513 | |
| 4514 | assert (kaabk_pctoindex (pw, pb, &a) && a == i); |
| 4515 | |
| 4516 | return; |
| 4517 | } |
| 4518 | |
| 4519 | static bool_t |
| 4520 | kaabk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out) |
| 4521 | { |
| 4522 | enum {N_WHITE = 4, N_BLACK = 1}; |
| 4523 | enum { |
| 4524 | BLOCK_B = 64, |
| 4525 | BLOCK_A = BLOCK_B * MAX_AAINDEX |
| 4526 | }; |
| 4527 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 4528 | index_t ki, ai; |
| 4529 | unsigned int ft; |
| 4530 | int i; |
| 4531 | |
| 4532 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4533 | |
| 4534 | assert (ft < 8); |
| 4535 | |
| 4536 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; ws[N_WHITE] = NOSQUARE; |
| 4537 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; bs[N_BLACK] = NOSQUARE; |
| 4538 | |
| 4539 | if ((ft & WE_FLAG) != 0) { |
| 4540 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 4541 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 4542 | } |
| 4543 | |
| 4544 | if ((ft & NS_FLAG) != 0) { |
| 4545 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 4546 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 4547 | } |
| 4548 | |
| 4549 | if ((ft & NW_SE_FLAG) != 0) { |
| 4550 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 4551 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 4552 | } |
| 4553 | |
| 4554 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4555 | ai = aaidx [ws[1]] [ws[2]]; |
| 4556 | |
| 4557 | if (IDX_is_empty(ki) || IDX_is_empty(ai)) { |
| 4558 | *out = NOINDEX; |
| 4559 | return FALSE; |
| 4560 | } |
| 4561 | *out = ki * BLOCK_A + ai * BLOCK_B + (index_t)ws[3]; |
| 4562 | return TRUE; |
| 4563 | } |
| 4564 | |
| 4565 | /****************** End KAAB/K *************************************/ |
| 4566 | |
| 4567 | /********************** KABB/K ************************************/ |
| 4568 | |
| 4569 | static bool_t test_kabbk (void); |
| 4570 | static bool_t kabbk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 4571 | static void kabbk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 4572 | |
| 4573 | static bool_t |
| 4574 | test_kabbk (void) |
| 4575 | { |
| 4576 | |
| 4577 | enum {MAXPC = 16+1}; |
| 4578 | char str[] = "kabbk"; |
| 4579 | SQUARE a, b, c, d, e; |
| 4580 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 4581 | SQUARE px[MAXPC], py[MAXPC]; |
| 4582 | |
| 4583 | index_t i, j; |
| 4584 | bool_t err = FALSE; |
| 4585 | |
| 4586 | printf ("%8s ", str); |
| 4587 | |
| 4588 | for (a = 0; a < 64; a++) { |
| 4589 | for (b = 0; b < 64; b++) { |
| 4590 | for (c = 0; c < 64; c++) { |
| 4591 | for (d = 0; d < 64; d++) { |
| 4592 | for (e = 0; e < 64; e++) { |
| 4593 | |
| 4594 | pw[0] = a; |
| 4595 | pw[1] = b; |
| 4596 | pw[2] = c; |
| 4597 | pw[3] = d; |
| 4598 | pw[4] = NOSQUARE; |
| 4599 | |
| 4600 | pb[0] = e; |
| 4601 | pb[1] = NOSQUARE; |
| 4602 | |
| 4603 | if (kabbk_pctoindex (pw, pb, &i)) { |
| 4604 | kabbk_indextopc (i, px, py); |
| 4605 | kabbk_pctoindex (px, py, &j); |
| 4606 | if (i != j) { |
| 4607 | err = TRUE; |
| 4608 | } |
| 4609 | assert (i == j); |
| 4610 | } |
| 4611 | |
| 4612 | } |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | |
| 4617 | if ((a&1)==0) { |
| 4618 | printf("."); |
| 4619 | fflush(stdout); |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | if (err) |
| 4624 | printf ("> %s NOT passed\n", str); |
| 4625 | else |
| 4626 | printf ("> %s PASSED\n", str); |
| 4627 | return !err; |
| 4628 | } |
| 4629 | |
| 4630 | static void |
| 4631 | kabbk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4632 | { |
| 4633 | enum { |
| 4634 | BLOCK_B = 64, |
| 4635 | BLOCK_A = BLOCK_B * MAX_AAINDEX |
| 4636 | }; |
| 4637 | index_t a, b, c, r, x, y; |
| 4638 | |
| 4639 | r = i; |
| 4640 | a = r / BLOCK_A; |
| 4641 | r -= a * BLOCK_A; |
| 4642 | |
| 4643 | b = r / BLOCK_B; |
| 4644 | r -= b * BLOCK_B; |
| 4645 | |
| 4646 | c = r; |
| 4647 | |
| 4648 | assert (i == (a * BLOCK_A + b * BLOCK_B + c)); |
| 4649 | |
| 4650 | pw[0] = wksq [a]; |
| 4651 | pb[0] = bksq [a]; |
| 4652 | |
| 4653 | x = aabase [b]; |
| 4654 | y = (b + 1) + x - (x * (127-x)/2); |
| 4655 | |
| 4656 | pw[1] = (SQUARE) c; |
| 4657 | pw[2] = (SQUARE) x; |
| 4658 | pw[3] = (SQUARE) y; |
| 4659 | pw[4] = NOSQUARE; |
| 4660 | |
| 4661 | pb[1] = NOSQUARE; |
| 4662 | |
| 4663 | assert (kabbk_pctoindex (pw, pb, &a) && a == i); |
| 4664 | |
| 4665 | return; |
| 4666 | } |
| 4667 | |
| 4668 | static bool_t |
| 4669 | kabbk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, /*@out@*/ index_t *out) |
| 4670 | { |
| 4671 | enum {N_WHITE = 4, N_BLACK = 1}; |
| 4672 | enum { |
| 4673 | BLOCK_B = 64, |
| 4674 | BLOCK_A = BLOCK_B * MAX_AAINDEX |
| 4675 | }; |
| 4676 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 4677 | index_t ki, ai; |
| 4678 | unsigned int ft; |
| 4679 | int i; |
| 4680 | |
| 4681 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4682 | |
| 4683 | assert (ft < 8); |
| 4684 | |
| 4685 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; ws[N_WHITE] = NOSQUARE; |
| 4686 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; bs[N_BLACK] = NOSQUARE; |
| 4687 | |
| 4688 | if ((ft & WE_FLAG) != 0) { |
| 4689 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 4690 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 4691 | } |
| 4692 | |
| 4693 | if ((ft & NS_FLAG) != 0) { |
| 4694 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 4695 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 4696 | } |
| 4697 | |
| 4698 | if ((ft & NW_SE_FLAG) != 0) { |
| 4699 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 4700 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 4701 | } |
| 4702 | |
| 4703 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4704 | ai = aaidx [ws[2]] [ws[3]]; |
| 4705 | |
| 4706 | if (IDX_is_empty(ki) || IDX_is_empty(ai)) { |
| 4707 | *out = NOINDEX; |
| 4708 | return FALSE; |
| 4709 | } |
| 4710 | *out = ki * BLOCK_A + ai * BLOCK_B + (index_t)ws[1]; |
| 4711 | return TRUE; |
| 4712 | } |
| 4713 | |
| 4714 | /********************** End KABB/K *************************************/ |
| 4715 | |
| 4716 | /********************** init KAAA/K ************************************/ |
| 4717 | |
| 4718 | static index_t |
| 4719 | aaa_getsubi (sq_t x, sq_t y, sq_t z); |
| 4720 | |
| 4721 | static sq_t aaa_xyz [MAX_AAAINDEX] [3]; |
| 4722 | static index_t aaa_base [64]; |
| 4723 | |
| 4724 | static index_t |
| 4725 | init_aaa (void) |
| 4726 | /* modifies aaa_base[], aaa_xyz[][] */ |
| 4727 | { |
| 4728 | index_t comb [64]; |
| 4729 | index_t accum; |
| 4730 | index_t a; |
| 4731 | |
| 4732 | index_t idx; |
| 4733 | SQUARE x, y, z; |
| 4734 | |
| 4735 | /* getting aaa_base */ |
| 4736 | comb [0] = 0; |
| 4737 | for (a = 1; a < 64; a++) { |
| 4738 | comb [a] = a * (a-1) / 2; |
| 4739 | } |
| 4740 | |
| 4741 | accum = 0; |
| 4742 | aaa_base [0] = accum; |
| 4743 | for (a = 0; a < (64-1); a++) { |
| 4744 | accum += comb[a]; |
| 4745 | aaa_base [a+1] = accum; |
| 4746 | } |
| 4747 | |
| 4748 | assert ((accum + comb[63]) == MAX_AAAINDEX); |
| 4749 | /* end getting aaa_base */ |
| 4750 | |
| 4751 | |
| 4752 | /* initialize aaa_xyz [][] */ |
| 4753 | for (idx = 0; idx < MAX_AAAINDEX; idx++) { |
| 4754 | IDX_set_empty (aaa_xyz[idx][0]); |
| 4755 | IDX_set_empty (aaa_xyz[idx][1]); |
| 4756 | IDX_set_empty (aaa_xyz[idx][2]); |
| 4757 | } |
| 4758 | |
| 4759 | idx = 0; |
| 4760 | for (z = 0; z < 64; z++) { |
| 4761 | for (y = 0; y < z; y++) { |
| 4762 | for (x = 0; x < y; x++) { |
| 4763 | |
| 4764 | assert (idx == aaa_getsubi (x, y, z)); |
| 4765 | |
| 4766 | aaa_xyz [idx] [0] = x; |
| 4767 | aaa_xyz [idx] [1] = y; |
| 4768 | aaa_xyz [idx] [2] = z; |
| 4769 | |
| 4770 | idx++; |
| 4771 | } |
| 4772 | } |
| 4773 | } |
| 4774 | |
| 4775 | assert (idx == MAX_AAAINDEX); |
| 4776 | |
| 4777 | return idx; |
| 4778 | } |
| 4779 | |
| 4780 | |
| 4781 | static index_t |
| 4782 | aaa_getsubi (sq_t x, sq_t y, sq_t z) |
| 4783 | /* uses aaa_base */ |
| 4784 | { |
| 4785 | index_t calc_idx, base; |
| 4786 | |
| 4787 | assert (x < 64 && y < 64 && z < 64); |
| 4788 | assert (x < y && y < z); |
| 4789 | |
| 4790 | base = aaa_base[z]; |
| 4791 | calc_idx = (index_t)x + ((index_t)y - 1) * (index_t)y / 2 + base; |
| 4792 | |
| 4793 | return calc_idx; |
| 4794 | } |
| 4795 | |
| 4796 | /********************** KAAA/K ************************************/ |
| 4797 | |
| 4798 | static bool_t test_kaaak (void); |
| 4799 | static bool_t kaaak_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 4800 | static void kaaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 4801 | |
| 4802 | static bool_t |
| 4803 | test_kaaak (void) |
| 4804 | { |
| 4805 | |
| 4806 | enum {MAXPC = 16+1}; |
| 4807 | char str[] = "kaaak"; |
| 4808 | SQUARE a, b, c, d, e; |
| 4809 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 4810 | SQUARE px[MAXPC], py[MAXPC]; |
| 4811 | |
| 4812 | index_t i, j; |
| 4813 | bool_t err = FALSE; |
| 4814 | |
| 4815 | printf ("%8s ", str); |
| 4816 | |
| 4817 | for (a = 0; a < 64; a++) { |
| 4818 | for (b = 0; b < 64; b++) { |
| 4819 | for (c = 0; c < 64; c++) { |
| 4820 | for (d = 0; d < 64; d++) { |
| 4821 | for (e = 0; e < 64; e++) { |
| 4822 | |
| 4823 | pw[0] = a; |
| 4824 | pw[1] = b; |
| 4825 | pw[2] = c; |
| 4826 | pw[3] = d; |
| 4827 | pw[4] = NOSQUARE; |
| 4828 | |
| 4829 | pb[0] = e; |
| 4830 | pb[1] = NOSQUARE; |
| 4831 | |
| 4832 | if (kaaak_pctoindex (pw, pb, &i)) { |
| 4833 | kaaak_indextopc (i, px, py); |
| 4834 | kaaak_pctoindex (px, py, &j); |
| 4835 | if (i != j) { |
| 4836 | err = TRUE; |
| 4837 | } |
| 4838 | assert (i == j); |
| 4839 | } |
| 4840 | |
| 4841 | } |
| 4842 | } |
| 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | if ((a&1)==0) { |
| 4847 | printf("."); |
| 4848 | fflush(stdout); |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | if (err) |
| 4853 | printf ("> %s NOT passed\n", str); |
| 4854 | else |
| 4855 | printf ("> %s PASSED\n", str); |
| 4856 | return !err; |
| 4857 | } |
| 4858 | |
| 4859 | static void |
| 4860 | kaaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 4861 | { |
| 4862 | enum { |
| 4863 | BLOCK_A = MAX_AAAINDEX |
| 4864 | }; |
| 4865 | index_t a, b, r; |
| 4866 | |
| 4867 | r = i; |
| 4868 | a = r / BLOCK_A; |
| 4869 | r -= a * BLOCK_A; |
| 4870 | |
| 4871 | b = r; |
| 4872 | |
| 4873 | assert (i == (a * BLOCK_A + b)); |
| 4874 | assert (b < BLOCK_A); |
| 4875 | |
| 4876 | pw[0] = wksq [a]; |
| 4877 | pb[0] = bksq [a]; |
| 4878 | |
| 4879 | pw[1] = aaa_xyz [b] [0]; |
| 4880 | pw[2] = aaa_xyz [b] [1]; |
| 4881 | pw[3] = aaa_xyz [b] [2]; |
| 4882 | pw[4] = NOSQUARE; |
| 4883 | |
| 4884 | pb[1] = NOSQUARE; |
| 4885 | |
| 4886 | assert (kaaak_pctoindex (pw, pb, &a) && a == i); |
| 4887 | |
| 4888 | return; |
| 4889 | } |
| 4890 | |
| 4891 | static bool_t |
| 4892 | kaaak_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 4893 | { |
| 4894 | enum {N_WHITE = 4, N_BLACK = 1}; |
| 4895 | enum { |
| 4896 | BLOCK_A = MAX_AAAINDEX |
| 4897 | }; |
| 4898 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 4899 | index_t ki, ai; |
| 4900 | unsigned int ft; |
| 4901 | int i; |
| 4902 | |
| 4903 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 4904 | |
| 4905 | assert (ft < 8); |
| 4906 | |
| 4907 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; ws[N_WHITE] = NOSQUARE; |
| 4908 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; bs[N_BLACK] = NOSQUARE; |
| 4909 | |
| 4910 | if ((ft & WE_FLAG) != 0) { |
| 4911 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 4912 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 4913 | } |
| 4914 | |
| 4915 | if ((ft & NS_FLAG) != 0) { |
| 4916 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 4917 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 4918 | } |
| 4919 | |
| 4920 | if ((ft & NW_SE_FLAG) != 0) { |
| 4921 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 4922 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 4923 | } |
| 4924 | |
| 4925 | |
| 4926 | { |
| 4927 | SQUARE tmp; |
| 4928 | if (ws[2] < ws[1]) { |
| 4929 | tmp = ws[1]; |
| 4930 | ws[1] = ws[2]; |
| 4931 | ws[2] = tmp; |
| 4932 | } |
| 4933 | if (ws[3] < ws[2]) { |
| 4934 | tmp = ws[2]; |
| 4935 | ws[2] = ws[3]; |
| 4936 | ws[3] = tmp; |
| 4937 | } |
| 4938 | if (ws[2] < ws[1]) { |
| 4939 | tmp = ws[1]; |
| 4940 | ws[1] = ws[2]; |
| 4941 | ws[2] = tmp; |
| 4942 | } |
| 4943 | } |
| 4944 | |
| 4945 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 4946 | |
| 4947 | /*128 == (128 & (((ws[1]^ws[2])-1) | ((ws[1]^ws[3])-1) | ((ws[2]^ws[3])-1)) */ |
| 4948 | |
| 4949 | if (ws[1] == ws[2] || ws[1] == ws[3] || ws[2] == ws[3]) { |
| 4950 | *out = NOINDEX; |
| 4951 | return FALSE; |
| 4952 | } |
| 4953 | |
| 4954 | ai = aaa_getsubi ( ws[1], ws[2], ws[3] ); |
| 4955 | |
| 4956 | if (IDX_is_empty(ki) || IDX_is_empty(ai)) { |
| 4957 | *out = NOINDEX; |
| 4958 | return FALSE; |
| 4959 | } |
| 4960 | *out = ki * BLOCK_A + ai; |
| 4961 | return TRUE; |
| 4962 | } |
| 4963 | |
| 4964 | /****************** End KAAB/K *************************************/ |
| 4965 | |
| 4966 | /********************** KAP/KB ************************************/ |
| 4967 | |
| 4968 | static bool_t test_kapkb (void); |
| 4969 | static bool_t kapkb_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 4970 | static void kapkb_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 4971 | |
| 4972 | static bool_t |
| 4973 | test_kapkb (void) |
| 4974 | { |
| 4975 | |
| 4976 | enum {MAXPC = 16+1}; |
| 4977 | char str[] = "kapkb"; |
| 4978 | SQUARE a, b, c, d, e; |
| 4979 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 4980 | SQUARE px[MAXPC], py[MAXPC]; |
| 4981 | |
| 4982 | index_t i, j; |
| 4983 | bool_t err = FALSE; |
| 4984 | |
| 4985 | printf ("%8s ", str); |
| 4986 | |
| 4987 | for (a = 0; a < 64; a++) { |
| 4988 | for (b = 0; b < 64; b++) { |
| 4989 | for (c = 0; c < 64; c++) { |
| 4990 | for (d = 0; d < 64; d++) { |
| 4991 | for (e = 0; e < 64; e++) { |
| 4992 | |
| 4993 | if (c <= H1 || c >= A8) |
| 4994 | continue; |
| 4995 | |
| 4996 | pw[0] = a; |
| 4997 | pw[1] = b; |
| 4998 | pw[2] = c; |
| 4999 | pw[3] = NOSQUARE; |
| 5000 | |
| 5001 | pb[0] = e; |
| 5002 | pb[1] = d; |
| 5003 | pb[2] = NOSQUARE; |
| 5004 | |
| 5005 | if (kapkb_pctoindex (pw, pb, &i)) { |
| 5006 | kapkb_indextopc (i, px, py); |
| 5007 | kapkb_pctoindex (px, py, &j); |
| 5008 | if (i != j) { |
| 5009 | err = TRUE; |
| 5010 | } |
| 5011 | assert (i == j); |
| 5012 | } |
| 5013 | |
| 5014 | } |
| 5015 | } |
| 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | if ((a&1)==0) { |
| 5020 | printf("."); |
| 5021 | fflush(stdout); |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | if (err) |
| 5026 | printf ("> %s NOT passed\n", str); |
| 5027 | else |
| 5028 | printf ("> %s PASSED\n", str); |
| 5029 | return !err; |
| 5030 | } |
| 5031 | |
| 5032 | static void |
| 5033 | kapkb_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5034 | { |
| 5035 | /*---------------------------------------------------------* |
| 5036 | inverse work to make sure that the following is valid |
| 5037 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d * BLOCK_D + e; |
| 5038 | *----------------------------------------------------------*/ |
| 5039 | enum {B11100 = 7u << 2}; |
| 5040 | enum {BLOCK_A = 64*64*64*64, BLOCK_B = 64*64*64, BLOCK_C = 64*64, BLOCK_D = 64}; |
| 5041 | index_t a, b, c, d, e, r; |
| 5042 | index_t x; |
| 5043 | |
| 5044 | r = i; |
| 5045 | a = r / BLOCK_A; |
| 5046 | r -= a * BLOCK_A; |
| 5047 | b = r / BLOCK_B; |
| 5048 | r -= b * BLOCK_B; |
| 5049 | c = r / BLOCK_C; |
| 5050 | r -= c * BLOCK_C; |
| 5051 | d = r / BLOCK_D; |
| 5052 | r -= d * BLOCK_D; |
| 5053 | e = r; |
| 5054 | |
| 5055 | /* x is pslice */ |
| 5056 | x = a; |
| 5057 | x += x & B11100; /* get upper part and double it */ |
| 5058 | x += 8; /* add extra row */ |
| 5059 | x ^= 070; /* flip NS */ |
| 5060 | |
| 5061 | pw[0] = (SQUARE) b; |
| 5062 | pb[0] = (SQUARE) c; |
| 5063 | pw[1] = (SQUARE) d; |
| 5064 | pw[2] = (SQUARE) x; |
| 5065 | pw[3] = NOSQUARE; |
| 5066 | pb[1] = (SQUARE) e; |
| 5067 | pb[2] = NOSQUARE; |
| 5068 | |
| 5069 | assert (kapkb_pctoindex (pw, pb, &a) && a == i); |
| 5070 | |
| 5071 | return; |
| 5072 | } |
| 5073 | |
| 5074 | |
| 5075 | static bool_t |
| 5076 | kapkb_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5077 | { |
| 5078 | enum {BLOCK_A = 64*64*64*64, BLOCK_B = 64*64*64, BLOCK_C = 64*64, BLOCK_D = 64}; |
| 5079 | index_t pslice; |
| 5080 | SQUARE sq; |
| 5081 | SQUARE pawn = pw[2]; |
| 5082 | SQUARE wa = pw[1]; |
| 5083 | SQUARE wk = pw[0]; |
| 5084 | SQUARE bk = pb[0]; |
| 5085 | SQUARE ba = pb[1]; |
| 5086 | |
| 5087 | assert (A2 <= pawn && pawn < A8); |
| 5088 | |
| 5089 | if ( !(A2 <= pawn && pawn < A8)) { |
| 5090 | *out = NOINDEX; |
| 5091 | return FALSE; |
| 5092 | } |
| 5093 | |
| 5094 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5095 | pawn = flipWE (pawn); |
| 5096 | wk = flipWE (wk); |
| 5097 | bk = flipWE (bk); |
| 5098 | wa = flipWE (wa); |
| 5099 | ba = flipWE (ba); |
| 5100 | } |
| 5101 | |
| 5102 | sq = pawn; |
| 5103 | sq ^= 070; /* flipNS*/ |
| 5104 | sq -= 8; /* down one row*/ |
| 5105 | pslice = (index_t) ((sq+(sq&3)) >> 1); |
| 5106 | |
| 5107 | *out = pslice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk * BLOCK_C + (index_t)wa * BLOCK_D + (index_t)ba; |
| 5108 | |
| 5109 | return TRUE; |
| 5110 | } |
| 5111 | /********************** end KAP/KB ************************************/ |
| 5112 | |
| 5113 | /************************* KAB/KP ************************************/ |
| 5114 | |
| 5115 | static bool_t test_kabkp (void); |
| 5116 | static bool_t kabkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 5117 | static void kabkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 5118 | |
| 5119 | static bool_t |
| 5120 | test_kabkp (void) |
| 5121 | { |
| 5122 | |
| 5123 | enum {MAXPC = 16+1}; |
| 5124 | char str[] = "kabkp"; |
| 5125 | SQUARE a, b, c, d, e; |
| 5126 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 5127 | SQUARE px[MAXPC], py[MAXPC]; |
| 5128 | |
| 5129 | index_t i, j; |
| 5130 | bool_t err = FALSE; |
| 5131 | |
| 5132 | printf ("%8s ", str); |
| 5133 | |
| 5134 | for (a = 0; a < 64; a++) { |
| 5135 | for (b = 0; b < 64; b++) { |
| 5136 | for (c = 0; c < 64; c++) { |
| 5137 | for (d = 0; d < 64; d++) { |
| 5138 | for (e = 0; e < 64; e++) { |
| 5139 | |
| 5140 | if (d <= H1 || d >= A8) |
| 5141 | continue; |
| 5142 | |
| 5143 | pw[0] = a; |
| 5144 | pw[1] = b; |
| 5145 | pw[2] = c; |
| 5146 | pw[3] = NOSQUARE; |
| 5147 | |
| 5148 | pb[0] = e; |
| 5149 | pb[1] = d; |
| 5150 | pb[2] = NOSQUARE; |
| 5151 | |
| 5152 | if (kabkp_pctoindex (pw, pb, &i)) { |
| 5153 | kabkp_indextopc (i, px, py); |
| 5154 | kabkp_pctoindex (px, py, &j); |
| 5155 | if (i != j) { |
| 5156 | err = TRUE; |
| 5157 | } |
| 5158 | assert (i == j); |
| 5159 | } |
| 5160 | |
| 5161 | } |
| 5162 | } |
| 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | if ((a&1)==0) { |
| 5167 | printf("."); |
| 5168 | fflush(stdout); |
| 5169 | } |
| 5170 | } |
| 5171 | |
| 5172 | if (err) |
| 5173 | printf ("> %s NOT passed\n", str); |
| 5174 | else |
| 5175 | printf ("> %s PASSED\n", str); |
| 5176 | return !err; |
| 5177 | } |
| 5178 | |
| 5179 | static void |
| 5180 | kabkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5181 | { |
| 5182 | /*---------------------------------------------------------* |
| 5183 | inverse work to make sure that the following is valid |
| 5184 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d * BLOCK_D + e; |
| 5185 | *----------------------------------------------------------*/ |
| 5186 | enum {B11100 = 7u << 2}; |
| 5187 | enum {BLOCK_A = 64*64*64*64, BLOCK_B = 64*64*64, BLOCK_C = 64*64, BLOCK_D = 64}; |
| 5188 | index_t a, b, c, d, e, r; |
| 5189 | index_t x; |
| 5190 | |
| 5191 | r = i; |
| 5192 | a = r / BLOCK_A; |
| 5193 | r -= a * BLOCK_A; |
| 5194 | b = r / BLOCK_B; |
| 5195 | r -= b * BLOCK_B; |
| 5196 | c = r / BLOCK_C; |
| 5197 | r -= c * BLOCK_C; |
| 5198 | d = r / BLOCK_D; |
| 5199 | r -= d * BLOCK_D; |
| 5200 | e = r; |
| 5201 | |
| 5202 | /* x is pslice */ |
| 5203 | x = a; |
| 5204 | x += x & B11100; /* get upper part and double it */ |
| 5205 | x += 8; /* add extra row */ |
| 5206 | /*x ^= 070;*/ /* do not flip NS */ |
| 5207 | |
| 5208 | pw[0] = (SQUARE) b; |
| 5209 | pb[0] = (SQUARE) c; |
| 5210 | pw[1] = (SQUARE) d; |
| 5211 | pw[2] = (SQUARE) e; |
| 5212 | pw[3] = NOSQUARE; |
| 5213 | pb[1] = (SQUARE) x; |
| 5214 | pb[2] = NOSQUARE; |
| 5215 | |
| 5216 | assert (kabkp_pctoindex (pw, pb, &a) && a == i); |
| 5217 | |
| 5218 | return; |
| 5219 | } |
| 5220 | |
| 5221 | |
| 5222 | static bool_t |
| 5223 | kabkp_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5224 | { |
| 5225 | enum {BLOCK_A = 64*64*64*64, BLOCK_B = 64*64*64, BLOCK_C = 64*64, BLOCK_D = 64}; |
| 5226 | index_t pslice; |
| 5227 | SQUARE sq; |
| 5228 | SQUARE pawn = pb[1]; |
| 5229 | SQUARE wa = pw[1]; |
| 5230 | SQUARE wk = pw[0]; |
| 5231 | SQUARE bk = pb[0]; |
| 5232 | SQUARE wb = pw[2]; |
| 5233 | |
| 5234 | assert (A2 <= pawn && pawn < A8); |
| 5235 | |
| 5236 | if ( !(A2 <= pawn && pawn < A8)) { |
| 5237 | *out = NOINDEX; |
| 5238 | return FALSE; |
| 5239 | } |
| 5240 | |
| 5241 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5242 | pawn = flipWE (pawn); |
| 5243 | wk = flipWE (wk); |
| 5244 | bk = flipWE (bk); |
| 5245 | wa = flipWE (wa); |
| 5246 | wb = flipWE (wb); |
| 5247 | } |
| 5248 | |
| 5249 | sq = pawn; |
| 5250 | /*sq ^= 070;*/ /* do not flipNS*/ |
| 5251 | sq -= 8; /* down one row*/ |
| 5252 | pslice = (index_t) ((sq+(sq&3)) >> 1); |
| 5253 | |
| 5254 | *out = pslice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk * BLOCK_C + (index_t)wa * BLOCK_D + (index_t)wb; |
| 5255 | |
| 5256 | return TRUE; |
| 5257 | } |
| 5258 | /********************** end KAB/KP ************************************/ |
| 5259 | |
| 5260 | |
| 5261 | static void |
| 5262 | kpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5263 | { |
| 5264 | /*---------------------------------------------------------* |
| 5265 | inverse work to make sure that the following is valid |
| 5266 | index = a * BLOCK_A + b * BLOCK_B + c; |
| 5267 | *----------------------------------------------------------*/ |
| 5268 | enum {B11100 = 7u << 2}; |
| 5269 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 5270 | index_t a, b, c, r; |
| 5271 | index_t x; |
| 5272 | |
| 5273 | r = i; |
| 5274 | a = r / BLOCK_A; |
| 5275 | r -= a * BLOCK_A; |
| 5276 | b = r / BLOCK_B; |
| 5277 | r -= b * BLOCK_B; |
| 5278 | c = r; |
| 5279 | |
| 5280 | /* x is pslice */ |
| 5281 | x = a; |
| 5282 | x += x & B11100; /* get upper part and double it */ |
| 5283 | x += 8; /* add extra row */ |
| 5284 | x ^= 070; /* flip NS */ |
| 5285 | |
| 5286 | pw[1] = (SQUARE) x; |
| 5287 | pw[0] = (SQUARE) b; |
| 5288 | pb[0] = (SQUARE) c; |
| 5289 | |
| 5290 | pw[2] = NOSQUARE; |
| 5291 | pb[1] = NOSQUARE; |
| 5292 | |
| 5293 | assert (kpk_pctoindex (pw, pb, &a) && a == i); |
| 5294 | |
| 5295 | return; |
| 5296 | } |
| 5297 | |
| 5298 | |
| 5299 | static bool_t |
| 5300 | kpk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5301 | { |
| 5302 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 5303 | index_t pslice; |
| 5304 | SQUARE sq; |
| 5305 | SQUARE pawn = pw[1]; |
| 5306 | SQUARE wk = pw[0]; |
| 5307 | SQUARE bk = pb[0]; |
| 5308 | |
| 5309 | #ifdef DEBUG |
| 5310 | if ( !(A2 <= pawn && pawn < A8)) { |
| 5311 | SQ_CONTENT wp[MAX_LISTSIZE], bp[MAX_LISTSIZE]; |
| 5312 | bp [0] = wp[0] = KING; |
| 5313 | wp[1] = PAWN; |
| 5314 | wp[2] = bp[1] = NOPIECE; |
| 5315 | output_state (0, pw, pb, wp, bp); |
| 5316 | } |
| 5317 | #endif |
| 5318 | |
| 5319 | assert (A2 <= pawn && pawn < A8); |
| 5320 | |
| 5321 | if ( !(A2 <= pawn && pawn < A8)) { |
| 5322 | *out = NOINDEX; |
| 5323 | return FALSE; |
| 5324 | } |
| 5325 | |
| 5326 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5327 | pawn = flipWE (pawn); |
| 5328 | wk = flipWE (wk); |
| 5329 | bk = flipWE (bk); |
| 5330 | } |
| 5331 | |
| 5332 | sq = pawn; |
| 5333 | sq ^= 070; /* flipNS*/ |
| 5334 | sq -= 8; /* down one row*/ |
| 5335 | pslice = (index_t) ((sq+(sq&3)) >> 1); |
| 5336 | |
| 5337 | *out = pslice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk; |
| 5338 | |
| 5339 | return TRUE; |
| 5340 | } |
| 5341 | |
| 5342 | |
| 5343 | /********************** KPP/K ************************************/ |
| 5344 | |
| 5345 | static bool_t test_kppk (void); |
| 5346 | static bool_t kppk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 5347 | static void kppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 5348 | |
| 5349 | static bool_t |
| 5350 | test_kppk (void) |
| 5351 | { |
| 5352 | |
| 5353 | enum {MAXPC = 16+1}; |
| 5354 | char str[] = "kppk"; |
| 5355 | SQUARE a, b, c, d; |
| 5356 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 5357 | SQUARE px[MAXPC], py[MAXPC]; |
| 5358 | |
| 5359 | index_t i, j; |
| 5360 | bool_t err = FALSE; |
| 5361 | |
| 5362 | printf ("%8s ", str); |
| 5363 | |
| 5364 | for (b = 0; b < 64; b++) { |
| 5365 | for (c = 0; c < 64; c++) { |
| 5366 | sq_t anchor1, anchor2, loosen1, loosen2; |
| 5367 | if (c <= H1 || c >= A8) |
| 5368 | continue; |
| 5369 | if (b <= H1 || b >= A8) |
| 5370 | continue; |
| 5371 | |
| 5372 | pp_putanchorfirst (b, c, &anchor1, &loosen1); |
| 5373 | pp_putanchorfirst (c, b, &anchor2, &loosen2); |
| 5374 | if (!(anchor1 == anchor2 && loosen1 == loosen2)) { |
| 5375 | printf ("Output depends on input in pp_outanchorfirst()\n input:%u, %u\n",(unsigned)b,(unsigned)c); |
| 5376 | fatal_error(); |
| 5377 | } |
| 5378 | } |
| 5379 | } |
| 5380 | |
| 5381 | |
| 5382 | for (a = 0; a < 64; a++) { |
| 5383 | for (b = 0; b < 64; b++) { |
| 5384 | for (c = 0; c < 64; c++) { |
| 5385 | for (d = 0; d < 64; d++) { |
| 5386 | |
| 5387 | |
| 5388 | if (c <= H1 || c >= A8) |
| 5389 | continue; |
| 5390 | if (b <= H1 || b >= A8) |
| 5391 | continue; |
| 5392 | |
| 5393 | |
| 5394 | pw[0] = a; |
| 5395 | pw[1] = b; |
| 5396 | pw[2] = c; |
| 5397 | pw[3] = NOSQUARE; |
| 5398 | |
| 5399 | pb[0] = d; |
| 5400 | pb[1] = NOSQUARE; |
| 5401 | |
| 5402 | if (kppk_pctoindex (pw, pb, &i)) { |
| 5403 | kppk_indextopc (i, px, py); |
| 5404 | kppk_pctoindex (px, py, &j); |
| 5405 | if (i != j) { |
| 5406 | err = TRUE; |
| 5407 | } |
| 5408 | assert (i == j); |
| 5409 | } |
| 5410 | |
| 5411 | |
| 5412 | } |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | if ((a&1)==0) { |
| 5417 | printf("."); |
| 5418 | fflush(stdout); |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | if (err) |
| 5423 | printf ("> %s NOT passed\n", str); |
| 5424 | else |
| 5425 | printf ("> %s PASSED\n", str); |
| 5426 | return !err; |
| 5427 | } |
| 5428 | |
| 5429 | static void |
| 5430 | kppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5431 | { |
| 5432 | /*---------------------------------------------------------* |
| 5433 | inverse work to make sure that the following is valid |
| 5434 | index = a * BLOCK_A + b * BLOCK_B + c; |
| 5435 | *----------------------------------------------------------*/ |
| 5436 | enum {B11100 = 7u << 2}; |
| 5437 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 5438 | index_t a, b, c, r; |
| 5439 | index_t m, n; |
| 5440 | |
| 5441 | r = i; |
| 5442 | a = r / BLOCK_A; |
| 5443 | r -= a * BLOCK_A; |
| 5444 | b = r / BLOCK_B; |
| 5445 | r -= b * BLOCK_B; |
| 5446 | c = r; |
| 5447 | |
| 5448 | m = pp_hi24 [a]; |
| 5449 | n = pp_lo48 [a]; |
| 5450 | |
| 5451 | pw[0] = (SQUARE) b; |
| 5452 | pb[0] = (SQUARE) c; |
| 5453 | pb[1] = NOSQUARE; |
| 5454 | |
| 5455 | pw[1] = pidx24_to_wsq (m); |
| 5456 | pw[2] = pidx48_to_wsq (n); |
| 5457 | |
| 5458 | pw[3] = NOSQUARE; |
| 5459 | |
| 5460 | |
| 5461 | assert (A2 <= pw[1] && pw[1] < A8); |
| 5462 | assert (A2 <= pw[2] && pw[2] < A8); |
| 5463 | |
| 5464 | #ifdef DEBUG |
| 5465 | if (!(kppk_pctoindex (pw, pb, &a) && a == i)) { |
| 5466 | pc_t wp[] = {KING, PAWN, PAWN, NOPIECE}; |
| 5467 | pc_t bp[] = {KING, NOPIECE}; |
| 5468 | printf("Indexes not matching: input:%d, output:%d\n", i, a); |
| 5469 | print_pos (pw, pb, wp, bp); |
| 5470 | } |
| 5471 | #endif |
| 5472 | |
| 5473 | assert (kppk_pctoindex (pw, pb, &a) && a == i); |
| 5474 | |
| 5475 | return; |
| 5476 | } |
| 5477 | |
| 5478 | |
| 5479 | static bool_t |
| 5480 | kppk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5481 | { |
| 5482 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 5483 | index_t pp_slice; |
| 5484 | SQUARE anchor, loosen; |
| 5485 | |
| 5486 | SQUARE wk = pw[0]; |
| 5487 | SQUARE pawn_a = pw[1]; |
| 5488 | SQUARE pawn_b = pw[2]; |
| 5489 | SQUARE bk = pb[0]; |
| 5490 | index_t i, j; |
| 5491 | |
| 5492 | #ifdef DEBUG |
| 5493 | if (!(A2 <= pawn_a && pawn_a < A8)) { |
| 5494 | printf ("\n\nsquare of pawn_a: %s\n", Square_str[pawn_a]); |
| 5495 | printf(" wk %s\n p1 %s\n p2 %s\n bk %s\n" |
| 5496 | , Square_str[wk] |
| 5497 | , Square_str[pawn_a] |
| 5498 | , Square_str[pawn_b] |
| 5499 | , Square_str[bk] |
| 5500 | ); |
| 5501 | } |
| 5502 | #endif |
| 5503 | |
| 5504 | assert (A2 <= pawn_a && pawn_a < A8); |
| 5505 | assert (A2 <= pawn_b && pawn_b < A8); |
| 5506 | |
| 5507 | pp_putanchorfirst (pawn_a, pawn_b, &anchor, &loosen); |
| 5508 | |
| 5509 | if ((anchor & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5510 | anchor = flipWE (anchor); |
| 5511 | loosen = flipWE (loosen); |
| 5512 | wk = flipWE (wk); |
| 5513 | bk = flipWE (bk); |
| 5514 | } |
| 5515 | |
| 5516 | i = wsq_to_pidx24 (anchor); |
| 5517 | j = wsq_to_pidx48 (loosen); |
| 5518 | |
| 5519 | pp_slice = ppidx [i] [j]; |
| 5520 | |
| 5521 | if (IDX_is_empty(pp_slice)) { |
| 5522 | *out = NOINDEX; |
| 5523 | return FALSE; |
| 5524 | } |
| 5525 | |
| 5526 | assert (pp_slice < MAX_PPINDEX ); |
| 5527 | |
| 5528 | *out = pp_slice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk; |
| 5529 | |
| 5530 | return TRUE; |
| 5531 | } |
| 5532 | /****************** end KPP/K ************************************/ |
| 5533 | |
| 5534 | static void |
| 5535 | kakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5536 | { |
| 5537 | /*---------------------------------------------------------* |
| 5538 | inverse work to make sure that the following is valid |
| 5539 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 5540 | *----------------------------------------------------------*/ |
| 5541 | enum {B11100 = 7u << 2}; |
| 5542 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 5543 | index_t a, b, c, d, r; |
| 5544 | index_t x; |
| 5545 | |
| 5546 | r = i; |
| 5547 | a = r / BLOCK_A; |
| 5548 | r -= a * BLOCK_A; |
| 5549 | b = r / BLOCK_B; |
| 5550 | r -= b * BLOCK_B; |
| 5551 | c = r / BLOCK_C; |
| 5552 | r -= c * BLOCK_C; |
| 5553 | d = r; |
| 5554 | |
| 5555 | /* x is pslice */ |
| 5556 | x = a; |
| 5557 | x += x & B11100; /* get upper part and double it */ |
| 5558 | x += 8; /* add extra row */ |
| 5559 | /* x ^= 070; */ /* flip NS */ |
| 5560 | |
| 5561 | pw[0] = (SQUARE) b; |
| 5562 | pb[0] = (SQUARE) c; |
| 5563 | pw[1] = (SQUARE) d; |
| 5564 | pb[1] = (SQUARE) x; |
| 5565 | pw[2] = NOSQUARE; |
| 5566 | pb[2] = NOSQUARE; |
| 5567 | |
| 5568 | assert (kakp_pctoindex (pw, pb, &a) && a == i); |
| 5569 | |
| 5570 | return; |
| 5571 | } |
| 5572 | |
| 5573 | |
| 5574 | static bool_t |
| 5575 | kakp_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5576 | { |
| 5577 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 5578 | index_t pslice; |
| 5579 | SQUARE sq; |
| 5580 | SQUARE pawn = pb[1]; |
| 5581 | SQUARE wa = pw[1]; |
| 5582 | SQUARE wk = pw[0]; |
| 5583 | SQUARE bk = pb[0]; |
| 5584 | |
| 5585 | assert (A2 <= pawn && pawn < A8); |
| 5586 | |
| 5587 | if ( !(A2 <= pawn && pawn < A8)) { |
| 5588 | *out = NOINDEX; |
| 5589 | return FALSE; |
| 5590 | } |
| 5591 | |
| 5592 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5593 | pawn = flipWE (pawn); |
| 5594 | wk = flipWE (wk); |
| 5595 | bk = flipWE (bk); |
| 5596 | wa = flipWE (wa); |
| 5597 | } |
| 5598 | |
| 5599 | sq = pawn; |
| 5600 | /*sq ^= 070;*/ /* flipNS*/ |
| 5601 | sq -= 8; /* down one row*/ |
| 5602 | pslice = (index_t) ((sq+(sq&3)) >> 1); |
| 5603 | |
| 5604 | *out = pslice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk * BLOCK_C + (index_t)wa; |
| 5605 | |
| 5606 | return TRUE; |
| 5607 | } |
| 5608 | |
| 5609 | |
| 5610 | static void |
| 5611 | kapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5612 | { |
| 5613 | /*---------------------------------------------------------* |
| 5614 | inverse work to make sure that the following is valid |
| 5615 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 5616 | *----------------------------------------------------------*/ |
| 5617 | enum {B11100 = 7u << 2}; |
| 5618 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 5619 | index_t a, b, c, d, r; |
| 5620 | index_t x; |
| 5621 | |
| 5622 | r = i; |
| 5623 | a = r / BLOCK_A; |
| 5624 | r -= a * BLOCK_A; |
| 5625 | b = r / BLOCK_B; |
| 5626 | r -= b * BLOCK_B; |
| 5627 | c = r / BLOCK_C; |
| 5628 | r -= c * BLOCK_C; |
| 5629 | d = r; |
| 5630 | |
| 5631 | /* x is pslice */ |
| 5632 | x = a; |
| 5633 | x += x & B11100; /* get upper part and double it */ |
| 5634 | x += 8; /* add extra row */ |
| 5635 | x ^= 070; /* flip NS */ |
| 5636 | |
| 5637 | pw[0] = (SQUARE) b; |
| 5638 | pb[0] = (SQUARE) c; |
| 5639 | pw[1] = (SQUARE) d; |
| 5640 | pw[2] = (SQUARE) x; |
| 5641 | pw[3] = NOSQUARE; |
| 5642 | pb[1] = NOSQUARE; |
| 5643 | |
| 5644 | assert (kapk_pctoindex (pw, pb, &a) && a == i); |
| 5645 | |
| 5646 | return; |
| 5647 | } |
| 5648 | |
| 5649 | |
| 5650 | static bool_t |
| 5651 | kapk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5652 | { |
| 5653 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 5654 | index_t pslice; |
| 5655 | SQUARE sq; |
| 5656 | SQUARE pawn = pw[2]; |
| 5657 | SQUARE wa = pw[1]; |
| 5658 | SQUARE wk = pw[0]; |
| 5659 | SQUARE bk = pb[0]; |
| 5660 | |
| 5661 | assert (A2 <= pawn && pawn < A8); |
| 5662 | |
| 5663 | if ( !(A2 <= pawn && pawn < A8)) { |
| 5664 | *out = NOINDEX; |
| 5665 | return FALSE; |
| 5666 | } |
| 5667 | |
| 5668 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5669 | pawn = flipWE (pawn); |
| 5670 | wk = flipWE (wk); |
| 5671 | bk = flipWE (bk); |
| 5672 | wa = flipWE (wa); |
| 5673 | } |
| 5674 | |
| 5675 | sq = pawn; |
| 5676 | sq ^= 070; /* flipNS*/ |
| 5677 | sq -= 8; /* down one row*/ |
| 5678 | pslice = (index_t) ((sq+(sq&3)) >> 1); |
| 5679 | |
| 5680 | *out = pslice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk * BLOCK_C + (index_t)wa; |
| 5681 | |
| 5682 | return TRUE; |
| 5683 | } |
| 5684 | |
| 5685 | |
| 5686 | static void |
| 5687 | kaak_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5688 | { |
| 5689 | enum {BLOCK_A = MAX_AAINDEX}; |
| 5690 | index_t a, b, r, x, y; |
| 5691 | |
| 5692 | r = i; |
| 5693 | a = r / BLOCK_A; |
| 5694 | r -= a * BLOCK_A; |
| 5695 | b = r; |
| 5696 | |
| 5697 | assert (i == (a * BLOCK_A + b)); |
| 5698 | |
| 5699 | pw[0] = wksq [a]; |
| 5700 | pb[0] = bksq [a]; |
| 5701 | |
| 5702 | x = aabase [b]; |
| 5703 | y = (b + 1) + x - (x * (127-x)/2); |
| 5704 | |
| 5705 | pw[1] = (SQUARE) x; |
| 5706 | pw[2] = (SQUARE) y; |
| 5707 | pw[3] = NOSQUARE; |
| 5708 | |
| 5709 | pb[1] = NOSQUARE; |
| 5710 | |
| 5711 | assert (kaak_pctoindex (pw, pb, &a) && a == i); |
| 5712 | |
| 5713 | return; |
| 5714 | } |
| 5715 | |
| 5716 | |
| 5717 | static bool_t |
| 5718 | kaak_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out) |
| 5719 | { |
| 5720 | enum {N_WHITE = 3, N_BLACK = 1}; |
| 5721 | enum {BLOCK_A = MAX_AAINDEX}; |
| 5722 | SQUARE ws[MAX_LISTSIZE], bs[MAX_LISTSIZE]; |
| 5723 | index_t ki, ai; |
| 5724 | unsigned int ft; |
| 5725 | SQUARE i; |
| 5726 | |
| 5727 | ft = flipt [inp_pb[0]] [inp_pw[0]]; |
| 5728 | |
| 5729 | assert (ft < 8); |
| 5730 | |
| 5731 | for (i = 0; i < N_WHITE; i++) ws[i] = inp_pw[i]; |
| 5732 | ws[N_WHITE] = NOSQUARE; |
| 5733 | for (i = 0; i < N_BLACK; i++) bs[i] = inp_pb[i]; |
| 5734 | bs[N_BLACK] = NOSQUARE; |
| 5735 | |
| 5736 | if ((ft & WE_FLAG) != 0) { |
| 5737 | for (i = 0; i < N_WHITE; i++) ws[i] = flipWE (ws[i]); |
| 5738 | for (i = 0; i < N_BLACK; i++) bs[i] = flipWE (bs[i]); |
| 5739 | } |
| 5740 | |
| 5741 | if ((ft & NS_FLAG) != 0) { |
| 5742 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNS (ws[i]); |
| 5743 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNS (bs[i]); |
| 5744 | } |
| 5745 | |
| 5746 | if ((ft & NW_SE_FLAG) != 0) { |
| 5747 | for (i = 0; i < N_WHITE; i++) ws[i] = flipNW_SE (ws[i]); |
| 5748 | for (i = 0; i < N_BLACK; i++) bs[i] = flipNW_SE (bs[i]); |
| 5749 | } |
| 5750 | |
| 5751 | ki = kkidx [bs[0]] [ws[0]]; /* kkidx [black king] [white king] */ |
| 5752 | ai = (index_t) aaidx [ws[1]] [ws[2]]; |
| 5753 | |
| 5754 | if (IDX_is_empty(ki) || IDX_is_empty(ai)) { |
| 5755 | *out = NOINDEX; |
| 5756 | return FALSE; |
| 5757 | } |
| 5758 | *out = ki * BLOCK_A + ai; |
| 5759 | return TRUE; |
| 5760 | } |
| 5761 | |
| 5762 | /********************** KPP/KA ************************************/ |
| 5763 | |
| 5764 | static bool_t test_kppka (void); |
| 5765 | static bool_t kppka_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 5766 | static void kppka_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 5767 | |
| 5768 | static bool_t |
| 5769 | test_kppka (void) |
| 5770 | { |
| 5771 | |
| 5772 | enum {MAXPC = 16+1}; |
| 5773 | char str[] = "kppka"; |
| 5774 | SQUARE a, b, c, d, e; |
| 5775 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 5776 | SQUARE px[MAXPC], py[MAXPC]; |
| 5777 | |
| 5778 | index_t i, j; |
| 5779 | bool_t err = FALSE; |
| 5780 | |
| 5781 | printf ("%8s ", str); |
| 5782 | |
| 5783 | for (a = 0; a < 64; a++) { |
| 5784 | for (b = 0; b < 64; b++) { |
| 5785 | for (c = 0; c < 64; c++) { |
| 5786 | for (d = 0; d < 64; d++) { |
| 5787 | for (e = 0; e < 64; e++) { |
| 5788 | |
| 5789 | if (c <= H1 || c >= A8) |
| 5790 | continue; |
| 5791 | if (b <= H1 || b >= A8) |
| 5792 | continue; |
| 5793 | |
| 5794 | |
| 5795 | pw[0] = a; |
| 5796 | pw[1] = b; |
| 5797 | pw[2] = c; |
| 5798 | pw[3] = NOSQUARE; |
| 5799 | |
| 5800 | pb[0] = e; |
| 5801 | pb[1] = d; |
| 5802 | pb[2] = NOSQUARE; |
| 5803 | |
| 5804 | if (kppka_pctoindex (pw, pb, &i)) { |
| 5805 | kppka_indextopc (i, px, py); |
| 5806 | kppka_pctoindex (px, py, &j); |
| 5807 | if (i != j) { |
| 5808 | err = TRUE; |
| 5809 | } |
| 5810 | assert (i == j); |
| 5811 | } |
| 5812 | |
| 5813 | } |
| 5814 | } |
| 5815 | } |
| 5816 | } |
| 5817 | |
| 5818 | if ((a&1)==0) { |
| 5819 | printf("."); |
| 5820 | fflush(stdout); |
| 5821 | } |
| 5822 | } |
| 5823 | |
| 5824 | if (err) |
| 5825 | printf ("> %s NOT passed\n", str); |
| 5826 | else |
| 5827 | printf ("> %s PASSED\n", str); |
| 5828 | return !err; |
| 5829 | } |
| 5830 | |
| 5831 | |
| 5832 | static void |
| 5833 | kppka_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5834 | { |
| 5835 | /*---------------------------------------------------------* |
| 5836 | inverse work to make sure that the following is valid |
| 5837 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 5838 | *----------------------------------------------------------*/ |
| 5839 | |
| 5840 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 5841 | index_t a, b, c, d, r; |
| 5842 | index_t m, n; |
| 5843 | |
| 5844 | r = i; |
| 5845 | a = r / BLOCK_A; |
| 5846 | r -= a * BLOCK_A; |
| 5847 | b = r / BLOCK_B; |
| 5848 | r -= b * BLOCK_B; |
| 5849 | c = r / BLOCK_C; |
| 5850 | r -= c * BLOCK_C; |
| 5851 | d = r; |
| 5852 | |
| 5853 | m = pp_hi24 [a]; |
| 5854 | n = pp_lo48 [a]; |
| 5855 | |
| 5856 | pw[0] = (SQUARE) b; |
| 5857 | pw[1] = pidx24_to_wsq (m); |
| 5858 | pw[2] = pidx48_to_wsq (n); |
| 5859 | pw[3] = NOSQUARE; |
| 5860 | |
| 5861 | pb[0] = (SQUARE) c; |
| 5862 | pb[1] = (SQUARE) d; |
| 5863 | pb[2] = NOSQUARE; |
| 5864 | |
| 5865 | |
| 5866 | assert (A2 <= pw[1] && pw[1] < A8); |
| 5867 | assert (A2 <= pw[2] && pw[2] < A8); |
| 5868 | assert (kppka_pctoindex (pw, pb, &a) && a == i); |
| 5869 | |
| 5870 | return; |
| 5871 | } |
| 5872 | |
| 5873 | |
| 5874 | static bool_t |
| 5875 | kppka_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 5876 | { |
| 5877 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 5878 | index_t pp_slice; |
| 5879 | index_t i, j; |
| 5880 | |
| 5881 | SQUARE anchor, loosen; |
| 5882 | |
| 5883 | SQUARE wk = pw[0]; |
| 5884 | SQUARE pawn_a = pw[1]; |
| 5885 | SQUARE pawn_b = pw[2]; |
| 5886 | SQUARE bk = pb[0]; |
| 5887 | SQUARE ba = pb[1]; |
| 5888 | |
| 5889 | |
| 5890 | assert (A2 <= pawn_a && pawn_a < A8); |
| 5891 | assert (A2 <= pawn_b && pawn_b < A8); |
| 5892 | |
| 5893 | pp_putanchorfirst (pawn_a, pawn_b, &anchor, &loosen); |
| 5894 | |
| 5895 | if ((anchor & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 5896 | anchor = flipWE (anchor); |
| 5897 | loosen = flipWE (loosen); |
| 5898 | wk = flipWE (wk); |
| 5899 | bk = flipWE (bk); |
| 5900 | ba = flipWE (ba); |
| 5901 | } |
| 5902 | |
| 5903 | i = wsq_to_pidx24 (anchor); |
| 5904 | j = wsq_to_pidx48 (loosen); |
| 5905 | |
| 5906 | pp_slice = ppidx [i] [j]; |
| 5907 | |
| 5908 | if (IDX_is_empty(pp_slice)) { |
| 5909 | *out = NOINDEX; |
| 5910 | return FALSE; |
| 5911 | } |
| 5912 | |
| 5913 | assert (pp_slice < MAX_PPINDEX ); |
| 5914 | |
| 5915 | *out = pp_slice * (index_t)BLOCK_A + (index_t)wk * (index_t)BLOCK_B + (index_t)bk * (index_t)BLOCK_C + (index_t)ba; |
| 5916 | |
| 5917 | return TRUE; |
| 5918 | } |
| 5919 | |
| 5920 | /********************** end KPP/KA ************************************/ |
| 5921 | |
| 5922 | /********************** KAPP/K ************************************/ |
| 5923 | |
| 5924 | static bool_t test_kappk (void); |
| 5925 | static bool_t kappk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 5926 | static void kappk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 5927 | |
| 5928 | static bool_t |
| 5929 | test_kappk (void) |
| 5930 | { |
| 5931 | |
| 5932 | enum {MAXPC = 16+1}; |
| 5933 | char str[] = "kappk"; |
| 5934 | SQUARE a, b, c, d, e; |
| 5935 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 5936 | SQUARE px[MAXPC], py[MAXPC]; |
| 5937 | |
| 5938 | index_t i, j; |
| 5939 | bool_t err = FALSE; |
| 5940 | |
| 5941 | printf ("%8s ", str); |
| 5942 | |
| 5943 | for (a = 0; a < 64; a++) { |
| 5944 | for (b = 0; b < 64; b++) { |
| 5945 | for (c = 0; c < 64; c++) { |
| 5946 | for (d = 0; d < 64; d++) { |
| 5947 | for (e = 0; e < 64; e++) { |
| 5948 | |
| 5949 | if (c <= H1 || c >= A8) |
| 5950 | continue; |
| 5951 | if (b <= H1 || b >= A8) |
| 5952 | continue; |
| 5953 | |
| 5954 | |
| 5955 | pw[0] = a; |
| 5956 | pw[1] = d; |
| 5957 | pw[2] = b; |
| 5958 | pw[3] = c; |
| 5959 | pw[4] = NOSQUARE; |
| 5960 | |
| 5961 | pb[0] = e; |
| 5962 | pb[1] = NOSQUARE; |
| 5963 | |
| 5964 | if (kappk_pctoindex (pw, pb, &i)) { |
| 5965 | kappk_indextopc (i, px, py); |
| 5966 | kappk_pctoindex (px, py, &j); |
| 5967 | if (i != j) { |
| 5968 | err = TRUE; |
| 5969 | } |
| 5970 | assert (i == j); |
| 5971 | } |
| 5972 | |
| 5973 | } |
| 5974 | } |
| 5975 | } |
| 5976 | } |
| 5977 | |
| 5978 | if ((a&1)==0) { |
| 5979 | printf("."); |
| 5980 | fflush(stdout); |
| 5981 | } |
| 5982 | } |
| 5983 | |
| 5984 | if (err) |
| 5985 | printf ("> %s NOT passed\n", str); |
| 5986 | else |
| 5987 | printf ("> %s PASSED\n", str); |
| 5988 | return !err; |
| 5989 | } |
| 5990 | |
| 5991 | |
| 5992 | static void |
| 5993 | kappk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 5994 | { |
| 5995 | /*---------------------------------------------------------* |
| 5996 | inverse work to make sure that the following is valid |
| 5997 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 5998 | *----------------------------------------------------------*/ |
| 5999 | |
| 6000 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 6001 | index_t a, b, c, d, r; |
| 6002 | index_t m, n; |
| 6003 | |
| 6004 | r = i; |
| 6005 | a = r / BLOCK_A; |
| 6006 | r -= a * BLOCK_A; |
| 6007 | b = r / BLOCK_B; |
| 6008 | r -= b * BLOCK_B; |
| 6009 | c = r / BLOCK_C; |
| 6010 | r -= c * BLOCK_C; |
| 6011 | d = r; |
| 6012 | |
| 6013 | m = pp_hi24 [a]; |
| 6014 | n = pp_lo48 [a]; |
| 6015 | |
| 6016 | pw[0] = (SQUARE) b; |
| 6017 | pw[1] = (SQUARE) d; |
| 6018 | pw[2] = pidx24_to_wsq (m); |
| 6019 | pw[3] = pidx48_to_wsq (n); |
| 6020 | pw[4] = NOSQUARE; |
| 6021 | |
| 6022 | pb[0] = (SQUARE) c; |
| 6023 | pb[1] = NOSQUARE; |
| 6024 | |
| 6025 | |
| 6026 | assert (A2 <= pw[3] && pw[3] < A8); |
| 6027 | assert (A2 <= pw[2] && pw[2] < A8); |
| 6028 | assert (kappk_pctoindex (pw, pb, &a) && a == i); |
| 6029 | |
| 6030 | return; |
| 6031 | } |
| 6032 | |
| 6033 | |
| 6034 | static bool_t |
| 6035 | kappk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 6036 | { |
| 6037 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 6038 | index_t pp_slice; |
| 6039 | SQUARE anchor, loosen; |
| 6040 | |
| 6041 | SQUARE wk = pw[0]; |
| 6042 | SQUARE wa = pw[1]; |
| 6043 | SQUARE pawn_a = pw[2]; |
| 6044 | SQUARE pawn_b = pw[3]; |
| 6045 | SQUARE bk = pb[0]; |
| 6046 | |
| 6047 | index_t i, j; |
| 6048 | |
| 6049 | assert (A2 <= pawn_a && pawn_a < A8); |
| 6050 | assert (A2 <= pawn_b && pawn_b < A8); |
| 6051 | |
| 6052 | pp_putanchorfirst (pawn_a, pawn_b, &anchor, &loosen); |
| 6053 | |
| 6054 | if ((anchor & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 6055 | anchor = flipWE (anchor); |
| 6056 | loosen = flipWE (loosen); |
| 6057 | wk = flipWE (wk); |
| 6058 | bk = flipWE (bk); |
| 6059 | wa = flipWE (wa); |
| 6060 | } |
| 6061 | |
| 6062 | i = wsq_to_pidx24 (anchor); |
| 6063 | j = wsq_to_pidx48 (loosen); |
| 6064 | |
| 6065 | pp_slice = ppidx [i] [j]; |
| 6066 | |
| 6067 | if (IDX_is_empty(pp_slice)) { |
| 6068 | *out = NOINDEX; |
| 6069 | return FALSE; |
| 6070 | } |
| 6071 | |
| 6072 | assert (pp_slice < MAX_PPINDEX ); |
| 6073 | |
| 6074 | *out = pp_slice * (index_t)BLOCK_A + (index_t)wk * (index_t)BLOCK_B + (index_t)bk * (index_t)BLOCK_C + (index_t)wa; |
| 6075 | |
| 6076 | return TRUE; |
| 6077 | } |
| 6078 | |
| 6079 | /********************** end KAPP/K ************************************/ |
| 6080 | |
| 6081 | /********************** KAPP/K ************************************/ |
| 6082 | |
| 6083 | static bool_t test_kapkp (void); |
| 6084 | static bool_t kapkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 6085 | static void kapkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 6086 | |
| 6087 | static bool_t |
| 6088 | test_kapkp (void) |
| 6089 | { |
| 6090 | |
| 6091 | enum {MAXPC = 16+1}; |
| 6092 | char str[] = "kapkp"; |
| 6093 | SQUARE a, b, c, d, e; |
| 6094 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 6095 | SQUARE px[MAXPC], py[MAXPC]; |
| 6096 | |
| 6097 | index_t i, j; |
| 6098 | bool_t err = FALSE; |
| 6099 | |
| 6100 | printf ("%8s ", str); |
| 6101 | |
| 6102 | for (a = 0; a < 64; a++) { |
| 6103 | for (b = 0; b < 64; b++) { |
| 6104 | for (c = 0; c < 64; c++) { |
| 6105 | for (d = 0; d < 64; d++) { |
| 6106 | for (e = 0; e < 64; e++) { |
| 6107 | |
| 6108 | if (c <= H1 || c >= A8) |
| 6109 | continue; |
| 6110 | if (b <= H1 || b >= A8) |
| 6111 | continue; |
| 6112 | |
| 6113 | |
| 6114 | pw[0] = a; |
| 6115 | pw[1] = d; |
| 6116 | pw[2] = b; |
| 6117 | pw[3] = NOSQUARE; |
| 6118 | |
| 6119 | pb[0] = e; |
| 6120 | pb[1] = c; |
| 6121 | pb[2] = NOSQUARE; |
| 6122 | |
| 6123 | if (kapkp_pctoindex (pw, pb, &i)) { |
| 6124 | kapkp_indextopc (i, px, py); |
| 6125 | kapkp_pctoindex (px, py, &j); |
| 6126 | if (i != j) { |
| 6127 | err = TRUE; |
| 6128 | } |
| 6129 | assert (i == j); |
| 6130 | } |
| 6131 | |
| 6132 | } |
| 6133 | } |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | if ((a&1)==0) { |
| 6138 | printf("."); |
| 6139 | fflush(stdout); |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | if (err) |
| 6144 | printf ("> %s NOT passed\n", str); |
| 6145 | else |
| 6146 | printf ("> %s PASSED\n", str); |
| 6147 | return !err; |
| 6148 | } |
| 6149 | |
| 6150 | |
| 6151 | static bool_t |
| 6152 | kapkp_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 6153 | { |
| 6154 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 6155 | index_t pp_slice; |
| 6156 | SQUARE anchor, loosen; |
| 6157 | |
| 6158 | SQUARE wk = pw[0]; |
| 6159 | SQUARE wa = pw[1]; |
| 6160 | SQUARE pawn_a = pw[2]; |
| 6161 | SQUARE bk = pb[0]; |
| 6162 | SQUARE pawn_b = pb[1]; |
| 6163 | index_t m, n; |
| 6164 | |
| 6165 | assert (A2 <= pawn_a && pawn_a < A8); |
| 6166 | assert (A2 <= pawn_b && pawn_b < A8); |
| 6167 | assert (pw[3] == NOSQUARE && pb[2] == NOSQUARE); |
| 6168 | |
| 6169 | anchor = pawn_a; |
| 6170 | loosen = pawn_b; |
| 6171 | |
| 6172 | if ((anchor & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 6173 | anchor = flipWE (anchor); |
| 6174 | loosen = flipWE (loosen); |
| 6175 | wk = flipWE (wk); |
| 6176 | bk = flipWE (bk); |
| 6177 | wa = flipWE (wa); |
| 6178 | } |
| 6179 | |
| 6180 | m = wsq_to_pidx24 (anchor); |
| 6181 | n = (index_t)loosen - 8; |
| 6182 | |
| 6183 | pp_slice = m * 48 + n; |
| 6184 | |
| 6185 | if (IDX_is_empty(pp_slice)) { |
| 6186 | *out = NOINDEX; |
| 6187 | return FALSE; |
| 6188 | } |
| 6189 | |
| 6190 | assert (pp_slice < (64*MAX_PpINDEX) ); |
| 6191 | |
| 6192 | *out = pp_slice * (index_t)BLOCK_A + (index_t)wk * (index_t)BLOCK_B + (index_t)bk * (index_t)BLOCK_C + (index_t)wa; |
| 6193 | |
| 6194 | return TRUE; |
| 6195 | } |
| 6196 | |
| 6197 | static void |
| 6198 | kapkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 6199 | { |
| 6200 | /*---------------------------------------------------------* |
| 6201 | inverse work to make sure that the following is valid |
| 6202 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 6203 | *----------------------------------------------------------*/ |
| 6204 | enum {BLOCK_A = 64*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 6205 | enum {block_m = 48}; |
| 6206 | index_t a, b, c, d, r; |
| 6207 | index_t m, n; |
| 6208 | SQUARE sq_m, sq_n; |
| 6209 | |
| 6210 | r = i; |
| 6211 | a = r / BLOCK_A; |
| 6212 | r -= a * BLOCK_A; |
| 6213 | b = r / BLOCK_B; |
| 6214 | r -= b * BLOCK_B; |
| 6215 | c = r / BLOCK_C; |
| 6216 | r -= c * BLOCK_C; |
| 6217 | d = r; |
| 6218 | |
| 6219 | /* unpack a, which is pslice, into m and n */ |
| 6220 | r = a; |
| 6221 | m = r / block_m; |
| 6222 | r -= m * block_m; |
| 6223 | n = r ; |
| 6224 | |
| 6225 | sq_m = pidx24_to_wsq (m); |
| 6226 | sq_n = (SQUARE) n + 8; |
| 6227 | |
| 6228 | pw[0] = (SQUARE) b; |
| 6229 | pb[0] = (SQUARE) c; |
| 6230 | pw[1] = (SQUARE) d; |
| 6231 | pw[2] = sq_m; |
| 6232 | pb[1] = sq_n; |
| 6233 | pw[3] = NOSQUARE; |
| 6234 | pb[2] = NOSQUARE; |
| 6235 | |
| 6236 | assert (A2 <= sq_m && sq_m < A8); |
| 6237 | assert (A2 <= sq_n && sq_n < A8); |
| 6238 | assert (kapkp_pctoindex (pw, pb, &a) && a == i); |
| 6239 | |
| 6240 | return; |
| 6241 | } |
| 6242 | |
| 6243 | /********************** end KAP/KP ************************************/ |
| 6244 | |
| 6245 | /********************** KABP/K ************************************/ |
| 6246 | |
| 6247 | static bool_t test_kabpk (void); |
| 6248 | static bool_t kabpk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 6249 | static void kabpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 6250 | |
| 6251 | static bool_t |
| 6252 | test_kabpk (void) |
| 6253 | { |
| 6254 | |
| 6255 | enum {MAXPC = 16+1}; |
| 6256 | char str[] = "kabpk"; |
| 6257 | SQUARE a, b, c, d, e; |
| 6258 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 6259 | SQUARE px[MAXPC], py[MAXPC]; |
| 6260 | |
| 6261 | index_t i, j; |
| 6262 | bool_t err = FALSE; |
| 6263 | |
| 6264 | printf ("%8s ", str); |
| 6265 | |
| 6266 | for (a = 0; a < 64; a++) { |
| 6267 | for (b = 0; b < 64; b++) { |
| 6268 | for (c = 0; c < 64; c++) { |
| 6269 | for (d = 0; d < 64; d++) { |
| 6270 | for (e = 0; e < 64; e++) { |
| 6271 | |
| 6272 | if (d <= H1 || d >= A8) |
| 6273 | continue; |
| 6274 | |
| 6275 | pw[0] = a; |
| 6276 | pw[1] = b; |
| 6277 | pw[2] = c; |
| 6278 | pw[3] = d; |
| 6279 | pw[4] = NOSQUARE; |
| 6280 | |
| 6281 | pb[0] = e; |
| 6282 | pb[1] = NOSQUARE; |
| 6283 | |
| 6284 | if (kabpk_pctoindex (pw, pb, &i)) { |
| 6285 | kabpk_indextopc (i, px, py); |
| 6286 | kabpk_pctoindex (px, py, &j); |
| 6287 | if (i != j) { |
| 6288 | err = TRUE; |
| 6289 | } |
| 6290 | assert (i == j); |
| 6291 | } |
| 6292 | |
| 6293 | } |
| 6294 | } |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | if ((a&1)==0) { |
| 6299 | printf("."); |
| 6300 | fflush(stdout); |
| 6301 | } |
| 6302 | } |
| 6303 | |
| 6304 | if (err) |
| 6305 | printf ("> %s NOT passed\n", str); |
| 6306 | else |
| 6307 | printf ("> %s PASSED\n", str); |
| 6308 | return !err; |
| 6309 | } |
| 6310 | |
| 6311 | |
| 6312 | static void |
| 6313 | kabpk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 6314 | { |
| 6315 | |
| 6316 | /*---------------------------------------------------------* |
| 6317 | inverse work to make sure that the following is valid |
| 6318 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d * BLOCK_D + e; |
| 6319 | *----------------------------------------------------------*/ |
| 6320 | enum {BLOCK_A = 64*64*64*64, BLOCK_B = 64*64*64, BLOCK_C = 64*64, BLOCK_D = 64}; |
| 6321 | index_t a, b, c, d, e, r; |
| 6322 | SQUARE x; |
| 6323 | |
| 6324 | r = i; |
| 6325 | a = r / BLOCK_A; |
| 6326 | r -= a * BLOCK_A; |
| 6327 | b = r / BLOCK_B; |
| 6328 | r -= b * BLOCK_B; |
| 6329 | c = r / BLOCK_C; |
| 6330 | r -= c * BLOCK_C; |
| 6331 | d = r / BLOCK_D; |
| 6332 | r -= d * BLOCK_D; |
| 6333 | e = r; |
| 6334 | |
| 6335 | x = pidx24_to_wsq(a); |
| 6336 | |
| 6337 | pw[0] = (SQUARE) b; |
| 6338 | pw[1] = (SQUARE) d; |
| 6339 | pw[2] = (SQUARE) e; |
| 6340 | pw[3] = x; |
| 6341 | pw[4] = NOSQUARE; |
| 6342 | |
| 6343 | pb[0] = (SQUARE) c; |
| 6344 | pb[1] = NOSQUARE; |
| 6345 | |
| 6346 | assert (kabpk_pctoindex (pw, pb, &a) && a == i); |
| 6347 | |
| 6348 | return; |
| 6349 | } |
| 6350 | |
| 6351 | |
| 6352 | static bool_t |
| 6353 | kabpk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 6354 | { |
| 6355 | enum {BLOCK_A = 64*64*64*64, BLOCK_B = 64*64*64, BLOCK_C = 64*64, BLOCK_D = 64}; |
| 6356 | index_t pslice; |
| 6357 | |
| 6358 | SQUARE wk = pw[0]; |
| 6359 | SQUARE wa = pw[1]; |
| 6360 | SQUARE wb = pw[2]; |
| 6361 | SQUARE pawn = pw[3]; |
| 6362 | SQUARE bk = pb[0]; |
| 6363 | |
| 6364 | assert (A2 <= pawn && pawn < A8); |
| 6365 | |
| 6366 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 6367 | pawn = flipWE (pawn); |
| 6368 | wk = flipWE (wk); |
| 6369 | bk = flipWE (bk); |
| 6370 | wa = flipWE (wa); |
| 6371 | wb = flipWE (wb); |
| 6372 | } |
| 6373 | |
| 6374 | pslice = wsq_to_pidx24 (pawn); |
| 6375 | |
| 6376 | *out = pslice * (index_t)BLOCK_A + (index_t)wk * (index_t)BLOCK_B + (index_t)bk * (index_t)BLOCK_C + (index_t)wa * (index_t)BLOCK_D + (index_t)wb; |
| 6377 | |
| 6378 | return TRUE; |
| 6379 | } |
| 6380 | |
| 6381 | /********************** end KABP/K ************************************/ |
| 6382 | |
| 6383 | /********************** KAAP/K ************************************/ |
| 6384 | |
| 6385 | static bool_t test_kaapk (void); |
| 6386 | static bool_t kaapk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 6387 | static void kaapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 6388 | |
| 6389 | static bool_t |
| 6390 | test_kaapk (void) |
| 6391 | { |
| 6392 | |
| 6393 | enum {MAXPC = 16+1}; |
| 6394 | char str[] = "kaapk"; |
| 6395 | SQUARE a, b, c, d, e; |
| 6396 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 6397 | SQUARE px[MAXPC], py[MAXPC]; |
| 6398 | |
| 6399 | index_t i, j; |
| 6400 | bool_t err = FALSE; |
| 6401 | |
| 6402 | printf ("%8s ", str); |
| 6403 | |
| 6404 | for (a = 0; a < 64; a++) { |
| 6405 | for (b = 0; b < 64; b++) { |
| 6406 | for (c = 0; c < 64; c++) { |
| 6407 | for (d = 0; d < 64; d++) { |
| 6408 | for (e = 0; e < 64; e++) { |
| 6409 | |
| 6410 | if (d <= H1 || d >= A8) |
| 6411 | continue; |
| 6412 | |
| 6413 | pw[0] = a; |
| 6414 | pw[1] = b; |
| 6415 | pw[2] = c; |
| 6416 | pw[3] = d; |
| 6417 | pw[4] = NOSQUARE; |
| 6418 | |
| 6419 | pb[0] = e; |
| 6420 | pb[1] = NOSQUARE; |
| 6421 | |
| 6422 | if (kaapk_pctoindex (pw, pb, &i)) { |
| 6423 | kaapk_indextopc (i, px, py); |
| 6424 | kaapk_pctoindex (px, py, &j); |
| 6425 | if (i != j) { |
| 6426 | err = TRUE; |
| 6427 | } |
| 6428 | assert (i == j); |
| 6429 | } |
| 6430 | |
| 6431 | } |
| 6432 | } |
| 6433 | } |
| 6434 | } |
| 6435 | |
| 6436 | if ((a&1)==0) { |
| 6437 | printf("."); |
| 6438 | fflush(stdout); |
| 6439 | } |
| 6440 | } |
| 6441 | |
| 6442 | if (err) |
| 6443 | printf ("> %s NOT passed\n", str); |
| 6444 | else |
| 6445 | printf ("> %s PASSED\n", str); |
| 6446 | return !err; |
| 6447 | } |
| 6448 | |
| 6449 | |
| 6450 | static void |
| 6451 | kaapk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 6452 | { |
| 6453 | /*---------------------------------------------------------* |
| 6454 | inverse work to make sure that the following is valid |
| 6455 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 6456 | *----------------------------------------------------------*/ |
| 6457 | enum {BLOCK_C = MAX_AAINDEX |
| 6458 | ,BLOCK_B = 64*BLOCK_C |
| 6459 | ,BLOCK_A = 64*BLOCK_B |
| 6460 | }; |
| 6461 | index_t a, b, c, d, r; |
| 6462 | index_t x, y, z; |
| 6463 | |
| 6464 | assert (i >= 0); |
| 6465 | |
| 6466 | r = i; |
| 6467 | a = r / BLOCK_A; |
| 6468 | r -= a * BLOCK_A; |
| 6469 | b = r / BLOCK_B; |
| 6470 | r -= b * BLOCK_B; |
| 6471 | c = r / BLOCK_C; |
| 6472 | r -= c * BLOCK_C; |
| 6473 | d = r; |
| 6474 | |
| 6475 | z = (index_t) pidx24_to_wsq(a); |
| 6476 | |
| 6477 | /* split d into x, y*/ |
| 6478 | x = aabase [d]; |
| 6479 | y = (d + 1) + x - (x * (127-x)/2); |
| 6480 | |
| 6481 | assert (aaidx[x][y] == aaidx[y][x]); |
| 6482 | assert (aaidx[x][y] == d); |
| 6483 | |
| 6484 | |
| 6485 | pw[0] = (SQUARE) b; |
| 6486 | pw[1] = (SQUARE) x; |
| 6487 | pw[2] = (SQUARE) y; |
| 6488 | pw[3] = (SQUARE) z; |
| 6489 | pw[4] = NOSQUARE; |
| 6490 | |
| 6491 | pb[0] = (SQUARE) c; |
| 6492 | pb[1] = NOSQUARE; |
| 6493 | |
| 6494 | assert (kaapk_pctoindex (pw, pb, &a) && a == i); |
| 6495 | |
| 6496 | return; |
| 6497 | } |
| 6498 | |
| 6499 | |
| 6500 | static bool_t |
| 6501 | kaapk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 6502 | { |
| 6503 | enum {BLOCK_C = MAX_AAINDEX |
| 6504 | ,BLOCK_B = 64*BLOCK_C |
| 6505 | ,BLOCK_A = 64*BLOCK_B |
| 6506 | }; |
| 6507 | index_t aa_combo, pslice; |
| 6508 | |
| 6509 | SQUARE wk = pw[0]; |
| 6510 | SQUARE wa = pw[1]; |
| 6511 | SQUARE wa2 = pw[2]; |
| 6512 | SQUARE pawn = pw[3]; |
| 6513 | SQUARE bk = pb[0]; |
| 6514 | |
| 6515 | assert (A2 <= pawn && pawn < A8); |
| 6516 | |
| 6517 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 6518 | pawn = flipWE (pawn); |
| 6519 | wk = flipWE (wk); |
| 6520 | bk = flipWE (bk); |
| 6521 | wa = flipWE (wa); |
| 6522 | wa2 = flipWE (wa2); |
| 6523 | } |
| 6524 | |
| 6525 | pslice = wsq_to_pidx24 (pawn); |
| 6526 | |
| 6527 | aa_combo = (index_t) aaidx [wa] [wa2]; |
| 6528 | |
| 6529 | if (IDX_is_empty(aa_combo)) { |
| 6530 | *out = NOINDEX; |
| 6531 | return FALSE; |
| 6532 | } |
| 6533 | |
| 6534 | *out = pslice * (index_t)BLOCK_A + (index_t)wk * (index_t)BLOCK_B + (index_t)bk * (index_t)BLOCK_C + aa_combo; |
| 6535 | |
| 6536 | assert (*out >= 0); |
| 6537 | |
| 6538 | return TRUE; |
| 6539 | } |
| 6540 | |
| 6541 | /********************** end KAAP/K ************************************/ |
| 6542 | |
| 6543 | /********************** KAA/KP ************************************/ |
| 6544 | |
| 6545 | static bool_t test_kaakp (void); |
| 6546 | static bool_t kaakp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 6547 | static void kaakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 6548 | |
| 6549 | static bool_t |
| 6550 | test_kaakp (void) |
| 6551 | { |
| 6552 | |
| 6553 | enum {MAXPC = 16+1}; |
| 6554 | char str[] = "kaakp"; |
| 6555 | SQUARE a, b, c, d, e; |
| 6556 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 6557 | SQUARE px[MAXPC], py[MAXPC]; |
| 6558 | |
| 6559 | index_t i, j; |
| 6560 | bool_t err = FALSE; |
| 6561 | |
| 6562 | printf ("%8s ", str); |
| 6563 | |
| 6564 | for (a = 0; a < 64; a++) { |
| 6565 | for (b = 0; b < 64; b++) { |
| 6566 | for (c = 0; c < 64; c++) { |
| 6567 | for (d = 0; d < 64; d++) { |
| 6568 | for (e = 0; e < 64; e++) { |
| 6569 | |
| 6570 | if (d <= H1 || d >= A8) |
| 6571 | continue; |
| 6572 | |
| 6573 | pw[0] = a; |
| 6574 | pw[1] = b; |
| 6575 | pw[2] = c; |
| 6576 | pw[3] = NOSQUARE; |
| 6577 | |
| 6578 | pb[0] = e; |
| 6579 | pb[1] = d; |
| 6580 | pb[2] = NOSQUARE; |
| 6581 | |
| 6582 | if (kaakp_pctoindex (pw, pb, &i)) { |
| 6583 | kaakp_indextopc (i, px, py); |
| 6584 | kaakp_pctoindex (px, py, &j); |
| 6585 | if (i != j) { |
| 6586 | err = TRUE; |
| 6587 | } |
| 6588 | assert (i == j); |
| 6589 | } |
| 6590 | |
| 6591 | } |
| 6592 | } |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | if ((a&1)==0) { |
| 6597 | printf("."); |
| 6598 | fflush(stdout); |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | if (err) |
| 6603 | printf ("> %s NOT passed\n", str); |
| 6604 | else |
| 6605 | printf ("> %s PASSED\n", str); |
| 6606 | return !err; |
| 6607 | } |
| 6608 | |
| 6609 | |
| 6610 | static void |
| 6611 | kaakp_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 6612 | { |
| 6613 | /*---------------------------------------------------------* |
| 6614 | inverse work to make sure that the following is valid |
| 6615 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 6616 | *----------------------------------------------------------*/ |
| 6617 | enum {BLOCK_C = MAX_AAINDEX |
| 6618 | ,BLOCK_B = 64*BLOCK_C |
| 6619 | ,BLOCK_A = 64*BLOCK_B |
| 6620 | }; |
| 6621 | index_t a, b, c, d, r; |
| 6622 | index_t x, y, z; |
| 6623 | SQUARE zq; |
| 6624 | |
| 6625 | assert (i >= 0); |
| 6626 | |
| 6627 | r = i; |
| 6628 | a = r / BLOCK_A; |
| 6629 | r -= a * BLOCK_A; |
| 6630 | b = r / BLOCK_B; |
| 6631 | r -= b * BLOCK_B; |
| 6632 | c = r / BLOCK_C; |
| 6633 | r -= c * BLOCK_C; |
| 6634 | d = r; |
| 6635 | |
| 6636 | zq = pidx24_to_wsq(a); |
| 6637 | z = (index_t)flipNS(zq); |
| 6638 | |
| 6639 | |
| 6640 | /* split d into x, y*/ |
| 6641 | x = aabase [d]; |
| 6642 | y = (d + 1) + x - (x * (127-x)/2); |
| 6643 | |
| 6644 | assert (aaidx[x][y] == aaidx[y][x]); |
| 6645 | assert (aaidx[x][y] == d); |
| 6646 | |
| 6647 | |
| 6648 | pw[0] = (SQUARE)b; |
| 6649 | pw[1] = (SQUARE)x; |
| 6650 | pw[2] = (SQUARE)y; |
| 6651 | pw[3] = NOSQUARE; |
| 6652 | |
| 6653 | pb[0] = (SQUARE)c; |
| 6654 | pb[1] = (SQUARE)z; |
| 6655 | pb[2] = NOSQUARE; |
| 6656 | |
| 6657 | assert (kaakp_pctoindex (pw, pb, &a) && a == i); |
| 6658 | |
| 6659 | return; |
| 6660 | } |
| 6661 | |
| 6662 | |
| 6663 | static bool_t |
| 6664 | kaakp_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 6665 | { |
| 6666 | enum {BLOCK_C = MAX_AAINDEX |
| 6667 | ,BLOCK_B = 64*BLOCK_C |
| 6668 | ,BLOCK_A = 64*BLOCK_B |
| 6669 | }; |
| 6670 | index_t aa_combo, pslice; |
| 6671 | |
| 6672 | SQUARE wk = pw[0]; |
| 6673 | SQUARE wa = pw[1]; |
| 6674 | SQUARE wa2 = pw[2]; |
| 6675 | SQUARE bk = pb[0]; |
| 6676 | SQUARE pawn = pb[1]; |
| 6677 | |
| 6678 | assert (A2 <= pawn && pawn < A8); |
| 6679 | |
| 6680 | if ((pawn & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 6681 | pawn = flipWE (pawn); |
| 6682 | wk = flipWE (wk); |
| 6683 | bk = flipWE (bk); |
| 6684 | wa = flipWE (wa); |
| 6685 | wa2 = flipWE (wa2); |
| 6686 | } |
| 6687 | |
| 6688 | pawn = flipNS(pawn); |
| 6689 | pslice = wsq_to_pidx24 (pawn); |
| 6690 | |
| 6691 | aa_combo = (index_t)aaidx [wa] [wa2]; |
| 6692 | |
| 6693 | if (IDX_is_empty(aa_combo)) { |
| 6694 | *out = NOINDEX; |
| 6695 | return FALSE; |
| 6696 | } |
| 6697 | |
| 6698 | *out = pslice * (index_t)BLOCK_A + (index_t)wk * (index_t)BLOCK_B + (index_t)bk * (index_t)BLOCK_C + aa_combo; |
| 6699 | |
| 6700 | assert (*out >= 0); |
| 6701 | |
| 6702 | return TRUE; |
| 6703 | } |
| 6704 | |
| 6705 | /********************** end KAA/KP ************************************/ |
| 6706 | |
| 6707 | /********************** KPP/KP ************************************/ |
| 6708 | /* |
| 6709 | index_t pp48_idx[48][48]; |
| 6710 | sq_t pp48_sq_x[MAX_PP48_INDEX]; |
| 6711 | sq_t pp48_sq_y[MAX_PP48_INDEX]; |
| 6712 | */ |
| 6713 | static bool_t test_kppkp (void); |
| 6714 | static bool_t kppkp_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 6715 | static void kppkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 6716 | |
| 6717 | static sq_t map24_b (sq_t s); |
| 6718 | static sq_t unmap24_b (index_t i); |
| 6719 | |
| 6720 | static index_t |
| 6721 | init_pp48_idx (void) |
| 6722 | /* modifies pp48_idx[][], pp48_sq_x[], pp48_sq_y[] */ |
| 6723 | { |
| 6724 | enum {MAX_I = 48, MAX_J = 48}; |
| 6725 | SQUARE i, j; |
| 6726 | index_t idx = 0; |
| 6727 | SQUARE a, b; |
| 6728 | |
| 6729 | /* default is noindex */ |
| 6730 | for (i = 0; i < MAX_I; i++) { |
| 6731 | for (j = 0; j < MAX_J; j++) { |
| 6732 | IDX_set_empty (pp48_idx [i][j]); |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | for (idx = 0; idx < MAX_PP48_INDEX; idx++) { |
| 6737 | pp48_sq_x [idx] = NOSQUARE; |
| 6738 | pp48_sq_y [idx] = NOSQUARE; |
| 6739 | } |
| 6740 | |
| 6741 | idx = 0; |
| 6742 | for (a = H7; a >= A2; a--) { |
| 6743 | |
| 6744 | for (b = a - 1; b >= A2; b--) { |
| 6745 | |
| 6746 | i = flipWE( flipNS (a) ) - 8; |
| 6747 | j = flipWE( flipNS (b) ) - 8; |
| 6748 | |
| 6749 | if (IDX_is_empty(pp48_idx [i] [j])) { |
| 6750 | |
| 6751 | pp48_idx [i][j]= idx; assert (idx < MAX_PP48_INDEX); |
| 6752 | pp48_idx [j][i]= idx; |
| 6753 | pp48_sq_x [idx] = i; assert (i < MAX_I); |
| 6754 | pp48_sq_y [idx] = j; assert (j < MAX_J); |
| 6755 | idx++; |
| 6756 | } |
| 6757 | } |
| 6758 | } |
| 6759 | assert (idx == MAX_PP48_INDEX); |
| 6760 | return idx; |
| 6761 | } |
| 6762 | |
| 6763 | |
| 6764 | |
| 6765 | static bool_t |
| 6766 | test_kppkp (void) |
| 6767 | { |
| 6768 | |
| 6769 | enum {MAXPC = 16+1}; |
| 6770 | char str[] = "kppkp"; |
| 6771 | SQUARE a, b, c, d, e; |
| 6772 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 6773 | SQUARE px[MAXPC], py[MAXPC]; |
| 6774 | |
| 6775 | index_t i, j; |
| 6776 | bool_t err = FALSE; |
| 6777 | |
| 6778 | printf ("%8s ", str); |
| 6779 | |
| 6780 | for (a = 0; a < 64; a++) { |
| 6781 | for (b = 0; b < 64; b++) { |
| 6782 | for (c = 0; c < 64; c++) { |
| 6783 | for (d = 0; d < 64; d++) { |
| 6784 | for (e = 0; e < 64; e++) { |
| 6785 | |
| 6786 | if (c <= H1 || c >= A8) |
| 6787 | continue; |
| 6788 | if (b <= H1 || b >= A8) |
| 6789 | continue; |
| 6790 | if (d <= H1 || d >= A8) |
| 6791 | continue; |
| 6792 | |
| 6793 | pw[0] = a; |
| 6794 | pw[1] = b; |
| 6795 | pw[2] = c; |
| 6796 | pw[3] = NOSQUARE; |
| 6797 | |
| 6798 | pb[0] = e; |
| 6799 | pb[1] = d; |
| 6800 | pb[2] = NOSQUARE; |
| 6801 | |
| 6802 | if (kppkp_pctoindex (pw, pb, &i)) { |
| 6803 | kppkp_indextopc (i, px, py); |
| 6804 | kppkp_pctoindex (px, py, &j); |
| 6805 | if (i != j) { |
| 6806 | err = TRUE; |
| 6807 | } |
| 6808 | assert (i == j); |
| 6809 | } |
| 6810 | |
| 6811 | } |
| 6812 | } |
| 6813 | } |
| 6814 | } |
| 6815 | |
| 6816 | if ((a&1)==0) { |
| 6817 | printf("."); |
| 6818 | fflush(stdout); |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | if (err) |
| 6823 | printf ("> %s NOT passed\n", str); |
| 6824 | else |
| 6825 | printf ("> %s PASSED\n", str); |
| 6826 | return !err; |
| 6827 | } |
| 6828 | |
| 6829 | |
| 6830 | static void |
| 6831 | kppkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 6832 | { |
| 6833 | /*---------------------------------------------------------* |
| 6834 | inverse work to make sure that the following is valid |
| 6835 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 6836 | *----------------------------------------------------------*/ |
| 6837 | |
| 6838 | enum {BLOCK_A = MAX_PP48_INDEX*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 6839 | index_t a, b, c, d, r; |
| 6840 | SQUARE m, n; |
| 6841 | |
| 6842 | r = i; |
| 6843 | a = r / BLOCK_A; |
| 6844 | r -= a * BLOCK_A; |
| 6845 | b = r / BLOCK_B; |
| 6846 | r -= b * BLOCK_B; |
| 6847 | c = r / BLOCK_C; |
| 6848 | r -= c * BLOCK_C; |
| 6849 | d = r; |
| 6850 | |
| 6851 | m = pp48_sq_x [b]; |
| 6852 | n = pp48_sq_y [b]; |
| 6853 | |
| 6854 | pw[0] = (SQUARE)c; |
| 6855 | pw[1] = flipWE(flipNS(m+8)); |
| 6856 | pw[2] = flipWE(flipNS(n+8)); |
| 6857 | pw[3] = NOSQUARE; |
| 6858 | |
| 6859 | pb[0] = (SQUARE)d; |
| 6860 | pb[1] = (SQUARE)unmap24_b (a); |
| 6861 | pb[2] = NOSQUARE; |
| 6862 | |
| 6863 | |
| 6864 | assert (A2 <= pw[1] && pw[1] < A8); |
| 6865 | assert (A2 <= pw[2] && pw[2] < A8); |
| 6866 | assert (A2 <= pb[1] && pb[1] < A8); |
| 6867 | assert (kppkp_pctoindex (pw, pb, &a) && a == i); |
| 6868 | |
| 6869 | return; |
| 6870 | } |
| 6871 | |
| 6872 | |
| 6873 | static bool_t |
| 6874 | kppkp_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 6875 | { |
| 6876 | enum {BLOCK_A = MAX_PP48_INDEX*64*64, BLOCK_B = 64*64, BLOCK_C = 64}; |
| 6877 | index_t pp48_slice; |
| 6878 | |
| 6879 | SQUARE wk = pw[0]; |
| 6880 | SQUARE pawn_a = pw[1]; |
| 6881 | SQUARE pawn_b = pw[2]; |
| 6882 | SQUARE bk = pb[0]; |
| 6883 | SQUARE pawn_c = pb[1]; |
| 6884 | SQUARE i, j, k; |
| 6885 | |
| 6886 | assert (A2 <= pawn_a && pawn_a < A8); |
| 6887 | assert (A2 <= pawn_b && pawn_b < A8); |
| 6888 | assert (A2 <= pawn_c && pawn_c < A8); |
| 6889 | |
| 6890 | if ((pawn_c & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 6891 | wk = flipWE (wk); |
| 6892 | pawn_a = flipWE (pawn_a); |
| 6893 | pawn_b = flipWE (pawn_b); |
| 6894 | bk = flipWE (bk); |
| 6895 | pawn_c = flipWE (pawn_c); |
| 6896 | } |
| 6897 | |
| 6898 | i = flipWE( flipNS (pawn_a) ) - 8; |
| 6899 | j = flipWE( flipNS (pawn_b) ) - 8; |
| 6900 | k = map24_b (pawn_c); /* black pawn, so low indexes mean more advanced 0 == A2 */ |
| 6901 | |
| 6902 | pp48_slice = pp48_idx [i] [j]; |
| 6903 | |
| 6904 | if (IDX_is_empty(pp48_slice)) { |
| 6905 | *out = NOINDEX; |
| 6906 | return FALSE; |
| 6907 | } |
| 6908 | |
| 6909 | assert (pp48_slice < MAX_PP48_INDEX ); |
| 6910 | |
| 6911 | *out = (index_t)k * (index_t)BLOCK_A + pp48_slice * (index_t)BLOCK_B + (index_t)wk * (index_t)BLOCK_C + (index_t)bk; |
| 6912 | |
| 6913 | return TRUE; |
| 6914 | } |
| 6915 | |
| 6916 | static sq_t |
| 6917 | map24_b (sq_t s) |
| 6918 | { |
| 6919 | s -= 8; |
| 6920 | return ((s&3)+s)>>1; |
| 6921 | } |
| 6922 | |
| 6923 | static sq_t |
| 6924 | unmap24_b (index_t i) |
| 6925 | { |
| 6926 | return (sq_t) ((i&(4+8+16)) + i + 8); |
| 6927 | } |
| 6928 | |
| 6929 | /********************** end KPP/KP ************************************/ |
| 6930 | |
| 6931 | /********************** KPPP/K ************************************/ |
| 6932 | |
| 6933 | static const sq_t itosq[48] = { |
| 6934 | H7,G7,F7,E7, |
| 6935 | H6,G6,F6,E6, |
| 6936 | H5,G5,F5,E5, |
| 6937 | H4,G4,F4,E4, |
| 6938 | H3,G3,F3,E3, |
| 6939 | H2,G2,F2,E2, |
| 6940 | D7,C7,B7,A7, |
| 6941 | D6,C6,B6,A6, |
| 6942 | D5,C5,B5,A5, |
| 6943 | D4,C4,B4,A4, |
| 6944 | D3,C3,B3,A3, |
| 6945 | D2,C2,B2,A2 |
| 6946 | }; |
| 6947 | |
| 6948 | static bool_t test_kpppk (void); |
| 6949 | static bool_t kpppk_pctoindex (const SQUARE *inp_pw, const SQUARE *inp_pb, index_t *out); |
| 6950 | static void kpppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb); |
| 6951 | |
| 6952 | static index_t |
| 6953 | init_ppp48_idx (void) |
| 6954 | /* modifies ppp48_idx[][], ppp48_sq_x[], ppp48_sq_y[], ppp48_sq_z[] */ |
| 6955 | { |
| 6956 | enum {MAX_I = 48, MAX_J = 48, MAX_K = 48}; |
| 6957 | SQUARE i, j, k; |
| 6958 | index_t idx = 0; |
| 6959 | SQUARE a, b, c; |
| 6960 | int x, y, z; |
| 6961 | |
| 6962 | /* default is noindex */ |
| 6963 | for (i = 0; i < MAX_I; i++) { |
| 6964 | for (j = 0; j < MAX_J; j++) { |
| 6965 | for (k = 0; k < MAX_K; k++) { |
| 6966 | IDX_set_empty(ppp48_idx [i][j][k]); |
| 6967 | } |
| 6968 | } |
| 6969 | } |
| 6970 | |
| 6971 | for (idx = 0; idx < MAX_PPP48_INDEX; idx++) { |
| 6972 | ppp48_sq_x [idx] = (uint8_t)NOSQUARE; |
| 6973 | ppp48_sq_y [idx] = (uint8_t)NOSQUARE; |
| 6974 | ppp48_sq_z [idx] = (uint8_t)NOSQUARE; |
| 6975 | } |
| 6976 | |
| 6977 | idx = 0; |
| 6978 | for (x = 0; x < 48; x++) { |
| 6979 | for (y = x+1; y < 48; y++) { |
| 6980 | for (z = y+1; z < 48; z++) { |
| 6981 | |
| 6982 | a = itosq [x]; |
| 6983 | b = itosq [y]; |
| 6984 | c = itosq [z]; |
| 6985 | |
| 6986 | if (!in_queenside(b) || !in_queenside(c)) |
| 6987 | continue; |
| 6988 | |
| 6989 | i = a - 8; |
| 6990 | j = b - 8; |
| 6991 | k = c - 8; |
| 6992 | |
| 6993 | if (IDX_is_empty(ppp48_idx [i] [j] [k])) { |
| 6994 | |
| 6995 | ppp48_idx [i][j][k] = idx; |
| 6996 | ppp48_idx [i][k][j] = idx; |
| 6997 | ppp48_idx [j][i][k] = idx; |
| 6998 | ppp48_idx [j][k][i] = idx; |
| 6999 | ppp48_idx [k][i][j] = idx; |
| 7000 | ppp48_idx [k][j][i] = idx; |
| 7001 | ppp48_sq_x [idx] = (uint8_t) i; assert (i < MAX_I); |
| 7002 | ppp48_sq_y [idx] = (uint8_t) j; assert (j < MAX_J); |
| 7003 | ppp48_sq_z [idx] = (uint8_t) k; assert (k < MAX_K); |
| 7004 | idx++; |
| 7005 | } |
| 7006 | } |
| 7007 | } |
| 7008 | } |
| 7009 | |
| 7010 | /* assert (idx == MAX_PPP48_INDEX);*/ |
| 7011 | return idx; |
| 7012 | } |
| 7013 | |
| 7014 | static bool_t |
| 7015 | test_kpppk (void) |
| 7016 | { |
| 7017 | |
| 7018 | enum {MAXPC = 16+1}; |
| 7019 | char str[] = "kpppk"; |
| 7020 | SQUARE a, b, c, d, e; |
| 7021 | SQUARE pw[MAXPC], pb[MAXPC]; |
| 7022 | SQUARE px[MAXPC], py[MAXPC]; |
| 7023 | |
| 7024 | index_t i, j; |
| 7025 | bool_t err = FALSE; |
| 7026 | |
| 7027 | printf ("%8s ", str); |
| 7028 | |
| 7029 | for (a = 0; a < 64; a++) { |
| 7030 | for (b = 0; b < 64; b++) { |
| 7031 | for (c = 0; c < 64; c++) { |
| 7032 | for (d = 0; d < 64; d++) { |
| 7033 | for (e = 0; e < 64; e++) { |
| 7034 | |
| 7035 | if (c <= H1 || c >= A8) |
| 7036 | continue; |
| 7037 | if (b <= H1 || b >= A8) |
| 7038 | continue; |
| 7039 | if (d <= H1 || d >= A8) |
| 7040 | continue; |
| 7041 | |
| 7042 | pw[0] = a; |
| 7043 | pw[1] = b; |
| 7044 | pw[2] = c; |
| 7045 | pw[3] = d; |
| 7046 | pw[4] = NOSQUARE; |
| 7047 | |
| 7048 | pb[0] = e; |
| 7049 | pb[1] = NOSQUARE; |
| 7050 | |
| 7051 | if (kpppk_pctoindex (pw, pb, &i)) { |
| 7052 | kpppk_indextopc (i, px, py); |
| 7053 | kpppk_pctoindex (px, py, &j); |
| 7054 | if (i != j) { |
| 7055 | err = TRUE; |
| 7056 | } |
| 7057 | assert (i == j); |
| 7058 | } |
| 7059 | |
| 7060 | } |
| 7061 | } |
| 7062 | } |
| 7063 | } |
| 7064 | |
| 7065 | if ((a&1)==0) { |
| 7066 | printf("."); |
| 7067 | fflush(stdout); |
| 7068 | } |
| 7069 | } |
| 7070 | |
| 7071 | if (err) |
| 7072 | printf ("> %s NOT passed\n", str); |
| 7073 | else |
| 7074 | printf ("> %s PASSED\n", str); |
| 7075 | return !err; |
| 7076 | } |
| 7077 | |
| 7078 | |
| 7079 | static void |
| 7080 | kpppk_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 7081 | { |
| 7082 | /*---------------------------------------------------------* |
| 7083 | inverse work to make sure that the following is valid |
| 7084 | index = a * BLOCK_A + b * BLOCK_B + c * BLOCK_C + d; |
| 7085 | *----------------------------------------------------------*/ |
| 7086 | |
| 7087 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 7088 | index_t a, b, c, r; |
| 7089 | SQUARE m, n, o; |
| 7090 | |
| 7091 | r = i; |
| 7092 | a = r / BLOCK_A; |
| 7093 | r -= a * BLOCK_A; |
| 7094 | b = r / BLOCK_B; |
| 7095 | r -= b * BLOCK_B; |
| 7096 | c = r; |
| 7097 | |
| 7098 | m = ppp48_sq_x [a]; |
| 7099 | n = ppp48_sq_y [a]; |
| 7100 | o = ppp48_sq_z [a]; |
| 7101 | |
| 7102 | |
| 7103 | pw[0] = (SQUARE)b; |
| 7104 | pw[1] = m + 8; |
| 7105 | pw[2] = n + 8; |
| 7106 | pw[3] = o + 8; |
| 7107 | pw[4] = NOSQUARE; |
| 7108 | |
| 7109 | pb[0] = (SQUARE)c; |
| 7110 | pb[1] = NOSQUARE; |
| 7111 | |
| 7112 | |
| 7113 | assert (A2 <= pw[1] && pw[1] < A8); |
| 7114 | assert (A2 <= pw[2] && pw[2] < A8); |
| 7115 | assert (A2 <= pw[3] && pw[3] < A8); |
| 7116 | assert (kpppk_pctoindex (pw, pb, &a) && a == i); |
| 7117 | |
| 7118 | return; |
| 7119 | } |
| 7120 | |
| 7121 | |
| 7122 | static bool_t |
| 7123 | kpppk_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 7124 | { |
| 7125 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 7126 | index_t ppp48_slice; |
| 7127 | |
| 7128 | SQUARE wk = pw[0]; |
| 7129 | SQUARE pawn_a = pw[1]; |
| 7130 | SQUARE pawn_b = pw[2]; |
| 7131 | SQUARE pawn_c = pw[3]; |
| 7132 | |
| 7133 | SQUARE bk = pb[0]; |
| 7134 | |
| 7135 | SQUARE i, j, k; |
| 7136 | |
| 7137 | assert (A2 <= pawn_a && pawn_a < A8); |
| 7138 | assert (A2 <= pawn_b && pawn_b < A8); |
| 7139 | assert (A2 <= pawn_c && pawn_c < A8); |
| 7140 | |
| 7141 | i = pawn_a - 8; |
| 7142 | j = pawn_b - 8; |
| 7143 | k = pawn_c - 8; |
| 7144 | |
| 7145 | ppp48_slice = ppp48_idx [i] [j] [k]; |
| 7146 | |
| 7147 | if (IDX_is_empty(ppp48_slice)) { |
| 7148 | wk = flipWE (wk); |
| 7149 | pawn_a = flipWE (pawn_a); |
| 7150 | pawn_b = flipWE (pawn_b); |
| 7151 | pawn_c = flipWE (pawn_c); |
| 7152 | bk = flipWE (bk); |
| 7153 | } |
| 7154 | |
| 7155 | i = pawn_a - 8; |
| 7156 | j = pawn_b - 8; |
| 7157 | k = pawn_c - 8; |
| 7158 | |
| 7159 | ppp48_slice = ppp48_idx [i] [j] [k]; |
| 7160 | |
| 7161 | if (IDX_is_empty(ppp48_slice)) { |
| 7162 | *out = NOINDEX; |
| 7163 | return FALSE; |
| 7164 | } |
| 7165 | |
| 7166 | assert (ppp48_slice < MAX_PPP48_INDEX ); |
| 7167 | |
| 7168 | *out = (index_t)ppp48_slice * BLOCK_A + (index_t)wk * BLOCK_B + (index_t)bk; |
| 7169 | |
| 7170 | return TRUE; |
| 7171 | } |
| 7172 | |
| 7173 | |
| 7174 | /********************** end KPPP/K ************************************/ |
| 7175 | |
| 7176 | |
| 7177 | static bool_t |
| 7178 | kpkp_pctoindex (const SQUARE *pw, const SQUARE *pb, index_t *out) |
| 7179 | { |
| 7180 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 7181 | SQUARE pp_slice; |
| 7182 | SQUARE anchor, loosen; |
| 7183 | |
| 7184 | SQUARE wk = pw[0]; |
| 7185 | SQUARE bk = pb[0]; |
| 7186 | SQUARE pawn_a = pw[1]; |
| 7187 | SQUARE pawn_b = pb[1]; |
| 7188 | |
| 7189 | SQUARE m, n; |
| 7190 | |
| 7191 | #ifdef DEBUG |
| 7192 | if (!(A2 <= pawn_a && pawn_a < A8)) { |
| 7193 | printf ("\n\nsquare of pawn_a: %s\n", Square_str[pawn_a]); |
| 7194 | printf(" wk %s\n p1 %s\n p2 %s\n bk %s\n" |
| 7195 | , Square_str[wk] |
| 7196 | , Square_str[pawn_a] |
| 7197 | , Square_str[pawn_b] |
| 7198 | , Square_str[bk] |
| 7199 | ); |
| 7200 | } |
| 7201 | #endif |
| 7202 | |
| 7203 | assert (A2 <= pawn_a && pawn_a < A8); |
| 7204 | assert (A2 <= pawn_b && pawn_b < A8); |
| 7205 | assert (pw[2] == NOSQUARE && pb[2] == NOSQUARE); |
| 7206 | |
| 7207 | /*pp_putanchorfirst (pawn_a, pawn_b, &anchor, &loosen);*/ |
| 7208 | anchor = pawn_a; |
| 7209 | loosen = pawn_b; |
| 7210 | |
| 7211 | if ((anchor & 07) > 3) { /* column is more than 3. e.g. = e,f,g, or h */ |
| 7212 | anchor = flipWE (anchor); |
| 7213 | loosen = flipWE (loosen); |
| 7214 | wk = flipWE (wk); |
| 7215 | bk = flipWE (bk); |
| 7216 | } |
| 7217 | |
| 7218 | m = (SQUARE)wsq_to_pidx24 (anchor); |
| 7219 | n = loosen - 8; |
| 7220 | |
| 7221 | pp_slice = m * 48 + n; |
| 7222 | |
| 7223 | if (IDX_is_empty(pp_slice)) { |
| 7224 | *out = NOINDEX; |
| 7225 | return FALSE; |
| 7226 | } |
| 7227 | |
| 7228 | assert (pp_slice < MAX_PpINDEX ); |
| 7229 | |
| 7230 | *out = (index_t) (pp_slice * BLOCK_A + wk * BLOCK_B + bk); |
| 7231 | |
| 7232 | return TRUE; |
| 7233 | } |
| 7234 | |
| 7235 | |
| 7236 | static void |
| 7237 | kpkp_indextopc (index_t i, SQUARE *pw, SQUARE *pb) |
| 7238 | { |
| 7239 | /*---------------------------------------------------------* |
| 7240 | inverse work to make sure that the following is valid |
| 7241 | index = a * BLOCK_A + b * BLOCK_B + c; |
| 7242 | *----------------------------------------------------------*/ |
| 7243 | enum {B11100 = 7u << 2}; |
| 7244 | enum {BLOCK_A = 64*64, BLOCK_B = 64}; |
| 7245 | enum {block_m = 48}; |
| 7246 | index_t a, b, c, r; |
| 7247 | index_t m, n; |
| 7248 | SQUARE sq_m, sq_n; |
| 7249 | |
| 7250 | r = i; |
| 7251 | a = r / BLOCK_A; |
| 7252 | r -= a * BLOCK_A; |
| 7253 | b = r / BLOCK_B; |
| 7254 | r -= b * BLOCK_B; |
| 7255 | c = r; |
| 7256 | |
| 7257 | /* unpack a, which is pslice, into m and n */ |
| 7258 | r = a; |
| 7259 | m = r / block_m; |
| 7260 | r -= m * block_m; |
| 7261 | n = r ; |
| 7262 | |
| 7263 | sq_m = pidx24_to_wsq (m); |
| 7264 | sq_n = (SQUARE)n + 8; |
| 7265 | |
| 7266 | pw[0] = (SQUARE)b; |
| 7267 | pb[0] = (SQUARE)c; |
| 7268 | pw[1] = sq_m; |
| 7269 | pb[1] = sq_n; |
| 7270 | pw[2] = NOSQUARE; |
| 7271 | pb[2] = NOSQUARE; |
| 7272 | |
| 7273 | assert (A2 <= pw[1] && pw[1] < A8); |
| 7274 | assert (A2 <= pb[1] && pb[1] < A8); |
| 7275 | |
| 7276 | return; |
| 7277 | } |
| 7278 | |
| 7279 | |
| 7280 | /****************************************************************************\ |
| 7281 | * |
| 7282 | * |
| 7283 | * DEBUG ZONE |
| 7284 | * |
| 7285 | * |
| 7286 | ****************************************************************************/ |
| 7287 | |
| 7288 | #if defined(DEBUG) |
| 7289 | static void |
| 7290 | print_pos (const sq_t *ws, const sq_t *bs, const pc_t *wp, const pc_t *bp) |
| 7291 | { |
| 7292 | int i; |
| 7293 | printf ("White: "); |
| 7294 | for (i = 0; ws[i] != NOSQUARE; i++) { |
| 7295 | printf ("%s%s ", P_str[wp[i]], Square_str[ws[i]]); |
| 7296 | } |
| 7297 | printf ("\nBlack: "); |
| 7298 | for (i = 0; bs[i] != NOSQUARE; i++) { |
| 7299 | printf ("%s%s ", P_str[bp[i]], Square_str[bs[i]]); |
| 7300 | } |
| 7301 | printf ("\n"); |
| 7302 | } |
| 7303 | #endif |
| 7304 | |
| 7305 | #if defined(DEBUG) || defined(FOLLOW_EGTB) |
| 7306 | static void |
| 7307 | output_state (unsigned stm, const SQUARE *wSQ, const SQUARE *bSQ, |
| 7308 | const SQ_CONTENT *wPC, const SQ_CONTENT *bPC) |
| 7309 | { |
| 7310 | int i; |
| 7311 | assert (stm == WH || stm == BL); |
| 7312 | |
| 7313 | printf("\n%s to move\n", stm==WH?"White":"Black"); |
| 7314 | printf("W: "); |
| 7315 | for (i = 0; wSQ[i] != NOSQUARE; i++) { |
| 7316 | printf("%s%s ", P_str[wPC[i]], Square_str[wSQ[i]]); |
| 7317 | } |
| 7318 | printf("\n"); |
| 7319 | printf("B: "); |
| 7320 | for (i = 0; bSQ[i] != NOSQUARE; i++) { |
| 7321 | printf("%s%s ", P_str[bPC[i]], Square_str[bSQ[i]]); |
| 7322 | } |
| 7323 | printf("\n\n"); |
| 7324 | } |
| 7325 | #endif |
| 7326 | |
| 7327 | static void |
| 7328 | list_index (void) |
| 7329 | { |
| 7330 | enum {START_GTB = 0, END_GTB = (MAX_EGKEYS)}; |
| 7331 | int i; |
| 7332 | index_t accum = 0; |
| 7333 | printf ("\nIndex for each GTB\n"); |
| 7334 | printf ("%3s: %7s %7s %7s %7s\n" , "i", "TB", "RAM-slice", "RAM-max", "HD-cumulative"); |
| 7335 | for (i = START_GTB; i < END_GTB; i++) { |
| 7336 | index_t indiv_k = egkey[i].maxindex * (index_t)sizeof(dtm_t) * 2/1024; |
| 7337 | accum += indiv_k; |
| 7338 | printf ("%3d: %7s %8luk %8luk %8luM\n", i, egkey[i].str, (long unsigned)(indiv_k/egkey[i].slice_n), |
| 7339 | (long unsigned)indiv_k, (long unsigned)accum/1024/2); |
| 7340 | } |
| 7341 | printf ("\n"); |
| 7342 | return; |
| 7343 | } |
| 7344 | |
| 7345 | /************************************************************************************************************** |
| 7346 | |
| 7347 | NEW_WDL |
| 7348 | |
| 7349 | **************************************************************************************************************/ |
| 7350 | |
| 7351 | /*---------------------------------------------------------------------*\ |
| 7352 | | WDL CACHE Implementation ZONE |
| 7353 | \*---------------------------------------------------------------------*/ |
| 7354 | |
| 7355 | /* |
| 7356 | | WDL CACHE Statics |
| 7357 | \*---------------------------------------------------------------------*/ |
| 7358 | |
| 7359 | /*--------------------------------------------------------------------------*/ |
| 7360 | static unsigned int wdl_extract (unit_t *uarr, index_t x); |
| 7361 | static wdl_block_t * wdl_point_block_to_replace (void); |
| 7362 | static void wdl_movetotop (wdl_block_t *t); |
| 7363 | |
| 7364 | #if 0 |
| 7365 | static bool_t wdl_cache_init (size_t cache_mem); |
| 7366 | static void wdl_cache_flush (void); |
| 7367 | static bool_t get_WDL (tbkey_t key, unsigned side, index_t idx, unsigned int *info_out, bool_t probe_hard_flag); |
| 7368 | #endif |
| 7369 | |
| 7370 | static bool_t wdl_cache_is_on (void); |
| 7371 | static void wdl_cache_reset_counters (void); |
| 7372 | static void wdl_cache_done (void); |
| 7373 | |
| 7374 | static wdl_block_t * wdl_point_block_to_replace (void); |
| 7375 | static bool_t get_WDL_from_cache (tbkey_t key, unsigned side, index_t idx, unsigned int *out); |
| 7376 | static void wdl_movetotop (wdl_block_t *t); |
| 7377 | static bool_t wdl_preload_cache (tbkey_t key, unsigned side, index_t idx); |
| 7378 | |
| 7379 | /*--------------------------------------------------------------------------*/ |
| 7380 | |
| 7381 | /*---------------------------------------------------------------------*\ |
| 7382 | | WDL CACHE Maintainance |
| 7383 | \*---------------------------------------------------------------------*/ |
| 7384 | |
| 7385 | |
| 7386 | static size_t |
| 7387 | wdl_cache_init (size_t cache_mem) |
| 7388 | { |
| 7389 | unsigned int i; |
| 7390 | wdl_block_t *p; |
| 7391 | size_t entries_per_block; |
| 7392 | size_t max_blocks; |
| 7393 | size_t block_mem; |
| 7394 | |
| 7395 | if (WDL_CACHE_INITIALIZED) |
| 7396 | wdl_cache_done(); |
| 7397 | |
| 7398 | entries_per_block = 16 * 1024; /* fixed, needed for the compression schemes */ |
| 7399 | |
| 7400 | WDL_units_per_block = entries_per_block / WDL_entries_per_unit; |
| 7401 | block_mem = WDL_units_per_block * sizeof(unit_t); |
| 7402 | |
| 7403 | max_blocks = cache_mem / block_mem; |
| 7404 | cache_mem = max_blocks * block_mem; |
| 7405 | |
| 7406 | |
| 7407 | wdl_cache_reset_counters (); |
| 7408 | |
| 7409 | wdl_cache.entries_per_block = entries_per_block; |
| 7410 | wdl_cache.max_blocks = max_blocks; |
| 7411 | wdl_cache.cached = TRUE; |
| 7412 | wdl_cache.top = NULL; |
| 7413 | wdl_cache.bot = NULL; |
| 7414 | wdl_cache.n = 0; |
| 7415 | |
| 7416 | if (0 == cache_mem || NULL == (wdl_cache.buffer = (unit_t *) malloc (cache_mem))) { |
| 7417 | wdl_cache.cached = FALSE; |
| 7418 | return 0; |
| 7419 | } |
| 7420 | |
| 7421 | if (0 == max_blocks|| NULL == (wdl_cache.blocks = (wdl_block_t *) malloc (max_blocks * sizeof(wdl_block_t)))) { |
| 7422 | wdl_cache.cached = FALSE; |
| 7423 | free (wdl_cache.buffer); |
| 7424 | return 0; |
| 7425 | } |
| 7426 | |
| 7427 | for (i = 0; i < max_blocks; i++) { |
| 7428 | p = &wdl_cache.blocks[i]; |
| 7429 | p->key = -1; |
| 7430 | p->side = gtbNOSIDE; |
| 7431 | p->offset = gtbNOINDEX; |
| 7432 | p->p_arr = wdl_cache.buffer + i * WDL_units_per_block; |
| 7433 | p->prev = NULL; |
| 7434 | p->next = NULL; |
| 7435 | } |
| 7436 | |
| 7437 | WDL_CACHE_INITIALIZED = TRUE; |
| 7438 | |
| 7439 | return cache_mem; |
| 7440 | } |
| 7441 | |
| 7442 | |
| 7443 | static void |
| 7444 | wdl_cache_done (void) |
| 7445 | { |
| 7446 | assert(WDL_CACHE_INITIALIZED); |
| 7447 | |
| 7448 | wdl_cache.cached = FALSE; |
| 7449 | wdl_cache.hard = 0; |
| 7450 | wdl_cache.soft = 0; |
| 7451 | wdl_cache.hardmisses = 0; |
| 7452 | wdl_cache.hits = 0; |
| 7453 | wdl_cache.softmisses = 0; |
| 7454 | wdl_cache.comparisons = 0; |
| 7455 | wdl_cache.max_blocks = 0; |
| 7456 | wdl_cache.entries_per_block = 0; |
| 7457 | |
| 7458 | wdl_cache.top = NULL; |
| 7459 | wdl_cache.bot = NULL; |
| 7460 | wdl_cache.n = 0; |
| 7461 | |
| 7462 | if (wdl_cache.buffer != NULL) |
| 7463 | free (wdl_cache.buffer); |
| 7464 | wdl_cache.buffer = NULL; |
| 7465 | |
| 7466 | if (wdl_cache.blocks != NULL) |
| 7467 | free (wdl_cache.blocks); |
| 7468 | wdl_cache.blocks = NULL; |
| 7469 | |
| 7470 | WDL_CACHE_INITIALIZED = FALSE; |
| 7471 | return; |
| 7472 | } |
| 7473 | |
| 7474 | |
| 7475 | static void |
| 7476 | wdl_cache_flush (void) |
| 7477 | { |
| 7478 | unsigned int i; |
| 7479 | wdl_block_t *p; |
| 7480 | size_t max_blocks = wdl_cache.max_blocks; |
| 7481 | |
| 7482 | wdl_cache.top = NULL; |
| 7483 | wdl_cache.bot = NULL; |
| 7484 | wdl_cache.n = 0; |
| 7485 | |
| 7486 | for (i = 0; i < max_blocks; i++) { |
| 7487 | p = &wdl_cache.blocks[i]; |
| 7488 | p->key = -1; |
| 7489 | p->side = gtbNOSIDE; |
| 7490 | p->offset = gtbNOINDEX; |
| 7491 | p->p_arr = wdl_cache.buffer + i * WDL_units_per_block; |
| 7492 | p->prev = NULL; |
| 7493 | p->next = NULL; |
| 7494 | } |
| 7495 | |
| 7496 | wdl_cache_reset_counters (); |
| 7497 | |
| 7498 | return; |
| 7499 | } |
| 7500 | |
| 7501 | |
| 7502 | static void |
| 7503 | wdl_cache_reset_counters (void) |
| 7504 | { |
| 7505 | wdl_cache.hard = 0; |
| 7506 | wdl_cache.soft = 0; |
| 7507 | wdl_cache.hardmisses = 0; |
| 7508 | wdl_cache.hits = 0; |
| 7509 | wdl_cache.softmisses = 0; |
| 7510 | wdl_cache.comparisons = 0; |
| 7511 | return; |
| 7512 | } |
| 7513 | |
| 7514 | |
| 7515 | static bool_t |
| 7516 | wdl_cache_is_on (void) |
| 7517 | { |
| 7518 | return wdl_cache.cached; |
| 7519 | } |
| 7520 | |
| 7521 | /****************************************************************************\ |
| 7522 | | Replacement |
| 7523 | \****************************************************************************/ |
| 7524 | |
| 7525 | static wdl_block_t * |
| 7526 | wdl_point_block_to_replace (void) |
| 7527 | { |
| 7528 | wdl_block_t *p, *t, *s; |
| 7529 | |
| 7530 | assert (0 == wdl_cache.n || wdl_cache.top != NULL); |
| 7531 | assert (0 == wdl_cache.n || wdl_cache.bot != NULL); |
| 7532 | assert (0 == wdl_cache.n || wdl_cache.bot->prev == NULL); |
| 7533 | assert (0 == wdl_cache.n || wdl_cache.top->next == NULL); |
| 7534 | |
| 7535 | if (wdl_cache.n > 0 && -1 == wdl_cache.top->key) { |
| 7536 | |
| 7537 | /* top blocks is unusable, should be the one to replace*/ |
| 7538 | p = wdl_cache.top; |
| 7539 | |
| 7540 | } else |
| 7541 | if (wdl_cache.n == 0) { |
| 7542 | |
| 7543 | p = &wdl_cache.blocks[wdl_cache.n++]; |
| 7544 | wdl_cache.top = p; |
| 7545 | wdl_cache.bot = p; |
| 7546 | |
| 7547 | p->prev = NULL; |
| 7548 | p->next = NULL; |
| 7549 | |
| 7550 | } else |
| 7551 | if (wdl_cache.n < wdl_cache.max_blocks) { /* add */ |
| 7552 | |
| 7553 | s = wdl_cache.top; |
| 7554 | p = &wdl_cache.blocks[wdl_cache.n++]; |
| 7555 | wdl_cache.top = p; |
| 7556 | |
| 7557 | s->next = p; |
| 7558 | p->prev = s; |
| 7559 | p->next = NULL; |
| 7560 | |
| 7561 | } else { /* replace*/ |
| 7562 | |
| 7563 | t = wdl_cache.bot; |
| 7564 | s = wdl_cache.top; |
| 7565 | wdl_cache.bot = t->next; |
| 7566 | wdl_cache.top = t; |
| 7567 | |
| 7568 | s->next = t; |
| 7569 | t->prev = s; |
| 7570 | wdl_cache.top->next = NULL; |
| 7571 | wdl_cache.bot->prev = NULL; |
| 7572 | |
| 7573 | p = t; |
| 7574 | } |
| 7575 | |
| 7576 | /* make the information content unusable, it will be replaced */ |
| 7577 | p->key = -1; |
| 7578 | p->side = gtbNOSIDE; |
| 7579 | p->offset = gtbNOINDEX; |
| 7580 | |
| 7581 | return p; |
| 7582 | } |
| 7583 | |
| 7584 | /****************************************************************************\ |
| 7585 | | |
| 7586 | | NEW PROBING ZONE |
| 7587 | | |
| 7588 | \****************************************************************************/ |
| 7589 | |
| 7590 | static unsigned int wdl_extract (unit_t *uarr, index_t x); |
| 7591 | static bool_t get_WDL_from_cache (tbkey_t key, unsigned side, index_t idx, unsigned int *info_out); |
| 7592 | static unsigned dtm2WDL(dtm_t dtm); |
| 7593 | static void wdl_movetotop (wdl_block_t *t); |
| 7594 | static bool_t wdl_preload_cache (tbkey_t key, unsigned side, index_t idx); |
| 7595 | static void dtm_block_2_wdl_block(dtm_block_t *g, wdl_block_t *w, size_t n); |
| 7596 | |
| 7597 | static bool_t |
| 7598 | get_WDL (tbkey_t key, unsigned side, index_t idx, unsigned int *info_out, bool_t probe_hard_flag) |
| 7599 | { |
| 7600 | dtm_t dtm; |
| 7601 | bool_t found; |
| 7602 | |
| 7603 | found = get_WDL_from_cache (key, side, idx, info_out); |
| 7604 | |
| 7605 | if (found) { |
| 7606 | wdl_cache.hits++; |
| 7607 | } else { |
| 7608 | /* may probe soft */ |
| 7609 | found = get_dtm (key, side, idx, &dtm, probe_hard_flag); |
| 7610 | if (found) { |
| 7611 | *info_out = dtm2WDL(dtm); |
| 7612 | /* move cache info from dtm_cache to WDL_cache */ |
| 7613 | if (wdl_cache_is_on()) |
| 7614 | wdl_preload_cache (key, side, idx); |
| 7615 | } |
| 7616 | } |
| 7617 | |
| 7618 | if (probe_hard_flag) { |
| 7619 | wdl_cache.hard++; |
| 7620 | if (!found) { |
| 7621 | wdl_cache.hardmisses++; |
| 7622 | } |
| 7623 | } else { |
| 7624 | wdl_cache.soft++; |
| 7625 | if (!found) { |
| 7626 | wdl_cache.softmisses++; |
| 7627 | } |
| 7628 | } |
| 7629 | |
| 7630 | return found; |
| 7631 | } |
| 7632 | |
| 7633 | static bool_t |
| 7634 | get_WDL_from_cache (tbkey_t key, unsigned side, index_t idx, unsigned int *out) |
| 7635 | { |
| 7636 | index_t offset; |
| 7637 | index_t remainder; |
| 7638 | wdl_block_t *p; |
| 7639 | wdl_block_t *ret; |
| 7640 | |
| 7641 | if (!wdl_cache_is_on()) |
| 7642 | return FALSE; |
| 7643 | |
| 7644 | split_index (wdl_cache.entries_per_block, idx, &offset, &remainder); |
| 7645 | |
| 7646 | ret = NULL; |
| 7647 | for (p = wdl_cache.top; p != NULL; p = p->prev) { |
| 7648 | |
| 7649 | wdl_cache.comparisons++; |
| 7650 | |
| 7651 | if (key == p->key && side == p->side && offset == p->offset) { |
| 7652 | ret = p; |
| 7653 | break; |
| 7654 | } |
| 7655 | } |
| 7656 | |
| 7657 | if (ret != NULL) { |
| 7658 | *out = wdl_extract (ret->p_arr, remainder); |
| 7659 | wdl_movetotop(ret); |
| 7660 | } |
| 7661 | |
| 7662 | FOLLOW_LU("get_wdl_from_cache ok?",(ret != NULL)) |
| 7663 | |
| 7664 | return ret != NULL; |
| 7665 | } |
| 7666 | |
| 7667 | static unsigned int |
| 7668 | wdl_extract (unit_t *uarr, index_t x) |
| 7669 | { |
| 7670 | index_t width = 2; |
| 7671 | index_t nu = x/WDL_entries_per_unit; |
| 7672 | index_t y = x - (nu * WDL_entries_per_unit); |
| 7673 | return (uarr[nu] >> (y*width)) & WDL_entry_mask; |
| 7674 | } |
| 7675 | |
| 7676 | static void |
| 7677 | wdl_movetotop (wdl_block_t *t) |
| 7678 | { |
| 7679 | wdl_block_t *s, *nx, *pv; |
| 7680 | |
| 7681 | assert (t != NULL); |
| 7682 | |
| 7683 | if (t->next == NULL) /* at the top already */ |
| 7684 | return; |
| 7685 | |
| 7686 | /* detach */ |
| 7687 | pv = t->prev; |
| 7688 | nx = t->next; |
| 7689 | |
| 7690 | if (pv == NULL) /* at the bottom */ |
| 7691 | wdl_cache.bot = nx; |
| 7692 | else |
| 7693 | pv->next = nx; |
| 7694 | |
| 7695 | if (nx == NULL) /* at the top */ |
| 7696 | wdl_cache.top = pv; |
| 7697 | else |
| 7698 | nx->prev = pv; |
| 7699 | |
| 7700 | /* relocate */ |
| 7701 | s = wdl_cache.top; |
| 7702 | assert (s != NULL); |
| 7703 | if (s == NULL) |
| 7704 | wdl_cache.bot = t; |
| 7705 | else |
| 7706 | s->next = t; |
| 7707 | |
| 7708 | t->next = NULL; |
| 7709 | t->prev = s; |
| 7710 | wdl_cache.top = t; |
| 7711 | |
| 7712 | return; |
| 7713 | } |
| 7714 | |
| 7715 | /****************************************************************************************************/ |
| 7716 | |
| 7717 | static bool_t |
| 7718 | wdl_preload_cache (tbkey_t key, unsigned side, index_t idx) |
| 7719 | /* output to the least used block of the cache */ |
| 7720 | { |
| 7721 | dtm_block_t *dtm_block; |
| 7722 | wdl_block_t *to_modify; |
| 7723 | bool_t ok; |
| 7724 | |
| 7725 | FOLLOW_label("wdl preload_cache starts") |
| 7726 | |
| 7727 | if (idx >= egkey[key].maxindex) { |
| 7728 | FOLLOW_LULU("Wrong index", __LINE__, idx) |
| 7729 | return FALSE; |
| 7730 | } |
| 7731 | |
| 7732 | /* find fresh block in dtm cache */ |
| 7733 | dtm_block = dtm_cache_pointblock (key, side, idx); |
| 7734 | |
| 7735 | /* find aged blocked in wdl cache */ |
| 7736 | to_modify = wdl_point_block_to_replace (); |
| 7737 | |
| 7738 | ok = !(NULL == dtm_block || NULL == to_modify); |
| 7739 | |
| 7740 | if (!ok) |
| 7741 | return FALSE; |
| 7742 | |
| 7743 | /* transform and move a block */ |
| 7744 | dtm_block_2_wdl_block(dtm_block, to_modify, dtm_cache.entries_per_block); |
| 7745 | |
| 7746 | if (ok) { |
| 7747 | index_t offset; |
| 7748 | index_t remainder; |
| 7749 | split_index (wdl_cache.entries_per_block, idx, &offset, &remainder); |
| 7750 | |
| 7751 | to_modify->key = key; |
| 7752 | to_modify->side = side; |
| 7753 | to_modify->offset = offset; |
| 7754 | } else { |
| 7755 | /* make it unusable */ |
| 7756 | to_modify->key = -1; |
| 7757 | to_modify->side = gtbNOSIDE; |
| 7758 | to_modify->offset = gtbNOINDEX; |
| 7759 | } |
| 7760 | |
| 7761 | FOLLOW_LU("wdl preload_cache?", ok) |
| 7762 | |
| 7763 | return ok; |
| 7764 | } |
| 7765 | |
| 7766 | /****************************************************************************************************/ |
| 7767 | |
| 7768 | static void |
| 7769 | dtm_block_2_wdl_block(dtm_block_t *g, wdl_block_t *w, size_t n) |
| 7770 | { |
| 7771 | int width = 2; |
| 7772 | int shifting; |
| 7773 | size_t i; |
| 7774 | int j; |
| 7775 | unsigned int x ,y; |
| 7776 | dtm_t *s = g->p_arr; |
| 7777 | unit_t *d = w->p_arr; |
| 7778 | |
| 7779 | for (i = 0, y = 0; i < n; i++) { |
| 7780 | j = i & 3; /* modulo WDL_entries_per_unit */ |
| 7781 | x = dtm2WDL(s[i]); |
| 7782 | shifting = j * width; |
| 7783 | y |= (x << shifting); |
| 7784 | |
| 7785 | if (j == 3) { |
| 7786 | d[i/WDL_entries_per_unit] = (unit_t) y; |
| 7787 | y = 0; |
| 7788 | } |
| 7789 | } |
| 7790 | |
| 7791 | if (0 != (n & 3)) { /* not multiple of 4 */ |
| 7792 | d[(n-1)/WDL_entries_per_unit] = (unit_t) y; /* save the rest */ |
| 7793 | y = 0; |
| 7794 | } |
| 7795 | |
| 7796 | return; |
| 7797 | } |
| 7798 | |
| 7799 | static unsigned |
| 7800 | dtm2WDL(dtm_t dtm) |
| 7801 | { |
| 7802 | return (unsigned) dtm & 3; |
| 7803 | } |
| 7804 | |
| 7805 | |
| 7806 | /**************************/ |
| 7807 | #ifdef WDL_PROBE |
| 7808 | |
| 7809 | static unsigned int inv_wdl(unsigned w); |
| 7810 | static bool_t egtb_get_wdl (tbkey_t k, unsigned stm, const SQUARE *wS, const SQUARE *bS, bool_t probe_hard_flag, unsigned int *wdl); |
| 7811 | |
| 7812 | static bool_t |
| 7813 | tb_probe_wdl |
| 7814 | (unsigned stm, |
| 7815 | const SQUARE *inp_wSQ, |
| 7816 | const SQUARE *inp_bSQ, |
| 7817 | const SQ_CONTENT *inp_wPC, |
| 7818 | const SQ_CONTENT *inp_bPC, |
| 7819 | bool_t probingtype, |
| 7820 | /*@out@*/ unsigned *res) |
| 7821 | { |
| 7822 | tbkey_t id = -1; |
| 7823 | unsigned int wdl = iUNKNOWN; |
| 7824 | |
| 7825 | SQUARE storage_ws [MAX_LISTSIZE], storage_bs [MAX_LISTSIZE]; |
| 7826 | SQ_CONTENT storage_wp [MAX_LISTSIZE], storage_bp [MAX_LISTSIZE]; |
| 7827 | |
| 7828 | SQUARE *ws = storage_ws; |
| 7829 | SQUARE *bs = storage_bs; |
| 7830 | SQ_CONTENT *wp = storage_wp; |
| 7831 | SQ_CONTENT *bp = storage_bp; |
| 7832 | SQUARE tmp_ws [MAX_LISTSIZE], tmp_bs [MAX_LISTSIZE]; |
| 7833 | SQ_CONTENT tmp_wp [MAX_LISTSIZE], tmp_bp [MAX_LISTSIZE]; |
| 7834 | |
| 7835 | SQUARE *temps; |
| 7836 | bool_t straight = FALSE; |
| 7837 | |
| 7838 | bool_t okcall = TRUE; |
| 7839 | unsigned ply_; |
| 7840 | unsigned *ply = &ply_; |
| 7841 | |
| 7842 | /************************************/ |
| 7843 | |
| 7844 | assert (stm == WH || stm == BL); |
| 7845 | |
| 7846 | /* VALID ONLY FOR KK!! */ |
| 7847 | if (inp_wPC[1] == NOPIECE && inp_bPC[1] == NOPIECE) { |
| 7848 | index_t dummy_i; |
| 7849 | bool_t b = kxk_pctoindex (inp_wSQ, inp_bSQ, &dummy_i); |
| 7850 | *res = b? iDRAW: iFORBID; |
| 7851 | *ply = 0; |
| 7852 | return TRUE; |
| 7853 | } |
| 7854 | |
| 7855 | /* copy input */ |
| 7856 | list_pc_copy (inp_wPC, wp); |
| 7857 | list_pc_copy (inp_bPC, bp); |
| 7858 | list_sq_copy (inp_wSQ, ws); |
| 7859 | list_sq_copy (inp_bSQ, bs); |
| 7860 | |
| 7861 | sortlists (ws, wp); |
| 7862 | sortlists (bs, bp); |
| 7863 | |
| 7864 | FOLLOW_label("EGTB_PROBE") |
| 7865 | |
| 7866 | if (egtb_get_id (wp, bp, &id)) { |
| 7867 | FOLLOW_LU("got ID",id) |
| 7868 | straight = TRUE; |
| 7869 | } else if (egtb_get_id (bp, wp, &id)) { |
| 7870 | FOLLOW_LU("rev ID",id) |
| 7871 | straight = FALSE; |
| 7872 | list_sq_flipNS (ws); |
| 7873 | list_sq_flipNS (bs); |
| 7874 | temps = ws; |
| 7875 | ws = bs; |
| 7876 | bs = temps; |
| 7877 | stm = Opp(stm); |
| 7878 | /* no enpassant in this fuction, so no adjustment */ |
| 7879 | {SQ_CONTENT *tempp = wp; wp = bp; bp = tempp;} /* added */ |
| 7880 | } else { |
| 7881 | #if defined(DEBUG) |
| 7882 | printf("did not get id...\n"); |
| 7883 | output_state (stm, ws, bs, wp, bp); |
| 7884 | #endif |
| 7885 | unpackdist (iFORBID, res, ply); |
| 7886 | return FALSE; |
| 7887 | } |
| 7888 | |
| 7889 | /* store position... */ |
| 7890 | list_pc_copy (wp, tmp_wp); |
| 7891 | list_pc_copy (bp, tmp_bp); |
| 7892 | list_sq_copy (ws, tmp_ws); |
| 7893 | list_sq_copy (bs, tmp_bs); |
| 7894 | |
| 7895 | /* x will be stm and y will be stw */ |
| 7896 | /* |
| 7897 | if (stm == WH) { |
| 7898 | xs = ws; |
| 7899 | xp = wp; |
| 7900 | ys = bs; |
| 7901 | yp = bp; |
| 7902 | } else { |
| 7903 | xs = bs; |
| 7904 | xp = bp; |
| 7905 | ys = ws; |
| 7906 | yp = wp; |
| 7907 | } |
| 7908 | */ |
| 7909 | okcall = egtb_get_wdl (id, stm, ws, bs, probingtype, &wdl); |
| 7910 | |
| 7911 | FOLLOW_LU("dtmok?",okcall) |
| 7912 | FOLLOW_DTM("wdl", wdl) |
| 7913 | |
| 7914 | if (okcall) { |
| 7915 | |
| 7916 | /*assert(epsq == NOSQUARE); */ |
| 7917 | |
| 7918 | if (straight) { |
| 7919 | *res = wdl; |
| 7920 | } else { |
| 7921 | *res = inv_wdl (wdl); |
| 7922 | } |
| 7923 | } else { |
| 7924 | unpackdist (iFORBID, res, ply); |
| 7925 | } |
| 7926 | |
| 7927 | return okcall; |
| 7928 | } |
| 7929 | |
| 7930 | static unsigned int |
| 7931 | inv_wdl(unsigned w) |
| 7932 | { |
| 7933 | unsigned r = tb_UNKNOWN; |
| 7934 | switch (w) { |
| 7935 | case tb_DRAW: r = tb_DRAW; break; |
| 7936 | case tb_WMATE: r = tb_BMATE; break; |
| 7937 | case tb_BMATE: r = tb_WMATE; break; |
| 7938 | case tb_FORBID: r = tb_FORBID; break; |
| 7939 | case tb_UNKNOWN: r = tb_UNKNOWN; break; |
| 7940 | default: r = tb_UNKNOWN; break; |
| 7941 | } |
| 7942 | return r; |
| 7943 | } |
| 7944 | |
| 7945 | static bool_t |
| 7946 | egtb_get_wdl (tbkey_t k, unsigned stm, const SQUARE *wS, const SQUARE *bS, bool_t probe_hard_flag, unsigned int *wdl) |
| 7947 | { |
| 7948 | bool_t idxavail; |
| 7949 | index_t idx; |
| 7950 | dtm_t *tab[2]; |
| 7951 | bool_t (*pc2idx) (const SQUARE *, const SQUARE *, index_t *); |
| 7952 | |
| 7953 | FOLLOW_label("egtb_get_wdl --> starts") |
| 7954 | |
| 7955 | if (egkey[k].status == STATUS_MALLOC || egkey[k].status == STATUS_STATICRAM) { |
| 7956 | |
| 7957 | tab[WH] = egkey[k].egt_w; |
| 7958 | tab[BL] = egkey[k].egt_b; |
| 7959 | pc2idx = egkey[k].pctoi; |
| 7960 | |
| 7961 | idxavail = pc2idx (wS, bS, &idx); |
| 7962 | |
| 7963 | FOLLOW_LU("indexavail (RAM)",idxavail) |
| 7964 | |
| 7965 | if (idxavail) { |
| 7966 | *wdl = dtm2WDL(tab[stm][idx]); |
| 7967 | } else { |
| 7968 | *wdl = dtm2WDL(iFORBID); |
| 7969 | } |
| 7970 | |
| 7971 | return FALSE; |
| 7972 | |
| 7973 | } else if (egkey[k].status == STATUS_ABSENT) { |
| 7974 | |
| 7975 | pc2idx = egkey[k].pctoi; |
| 7976 | idxavail = pc2idx (wS, bS, &idx); |
| 7977 | |
| 7978 | FOLLOW_LU("indexavail (HD)",idxavail) |
| 7979 | |
| 7980 | if (idxavail) { |
| 7981 | bool_t success; |
| 7982 | |
| 7983 | /* |
| 7984 | | LOCK |
| 7985 | *-------------------------------*/ |
| 7986 | mythread_mutex_lock (&Egtb_lock); |
| 7987 | |
| 7988 | success = get_WDL (k, stm, idx, wdl, probe_hard_flag); |
| 7989 | FOLLOW_LU("get_wld (succ)",success) |
| 7990 | FOLLOW_LU("get_wld (wdl )",*wdl) |
| 7991 | |
| 7992 | /* this may not be needed */ |
| 7993 | if (!success) { |
| 7994 | dtm_t dtm; |
| 7995 | unsigned res, ply; |
| 7996 | if (probe_hard_flag && Uncompressed) { |
| 7997 | assert(Uncompressed); |
| 7998 | success = egtb_filepeek (k, stm, idx, &dtm); |
| 7999 | unpackdist (dtm, &res, &ply); |
| 8000 | *wdl = res; |
| 8001 | } |
| 8002 | else |
| 8003 | success = FALSE; |
| 8004 | } |
| 8005 | |
| 8006 | mythread_mutex_unlock (&Egtb_lock); |
| 8007 | /*------------------------------*\ |
| 8008 | | UNLOCK |
| 8009 | */ |
| 8010 | |
| 8011 | if (success) { |
| 8012 | return TRUE; |
| 8013 | } else { |
| 8014 | if (probe_hard_flag) /* after probing hard and failing, no chance to succeed later */ |
| 8015 | egkey[k].status = STATUS_REJECT; |
| 8016 | *wdl = dtm2WDL(iUNKNOWN); |
| 8017 | return FALSE; |
| 8018 | } |
| 8019 | |
| 8020 | } else { |
| 8021 | *wdl = dtm2WDL(iFORBID); |
| 8022 | return TRUE; |
| 8023 | } |
| 8024 | } else if (egkey[k].status == STATUS_REJECT) { |
| 8025 | FOLLOW_label("STATUS_REJECT") |
| 8026 | *wdl = dtm2WDL(iFORBID); |
| 8027 | return FALSE; |
| 8028 | } else { |
| 8029 | FOLLOW_label("STATUS_WRONG!") |
| 8030 | assert(0); |
| 8031 | *wdl = dtm2WDL(iFORBID); |
| 8032 | return FALSE; |
| 8033 | } |
| 8034 | |
| 8035 | } |
| 8036 | #endif |
| 8037 | |
| 8038 | |
| 8039 |