internal static void setup_pieces_pawn(TBEntry_pawn ptr, byte[] data, ulong[] tb_size, int f) { int i; int j; int order; int order2; j = 1 + (ptr.pawns[1] > 0); order = data[0] & 0x0f; order2 = ptr.pawns[1] != 0 ? (data[1] & 0x0f) : 0x0f; for (i = 0; i < ptr.num; i++) { ptr.file[f].pieces[0, i] = (byte)(data[i + j] & 0x0f); } GlobalMembersTbcore.set_norm_pawn(ptr, ptr.file[f].norm[0], ptr.file[f].pieces[0]); tb_size[0] = GlobalMembersTbcore.calc_factors_pawn(ptr.file[f].factor[0], ptr.num, order, order2, ptr.file[f].norm[0], f); order = data[0] >> 4; order2 = ptr.pawns[1] != 0 ? (data[1] >> 4) : 0x0f; for (i = 0; i < ptr.num; i++) { ptr.file[f].pieces[1, i] = (byte)(data[i + j] >> 4); } GlobalMembersTbcore.set_norm_pawn(ptr, ptr.file[f].norm[1], ptr.file[f].pieces[1]); tb_size[1] = GlobalMembersTbcore.calc_factors_pawn(ptr.file[f].factor[1], ptr.num, order, order2, ptr.file[f].norm[1], f); }
//static ulong calc_key_from_pcs(ref int pcs, int mirror); internal static void free_wdl_entry(TBEntry entry) { GlobalMembersTbcore.unmap_file(ref entry.data, entry.mapping); if (entry.has_pawns == 0) { TBEntry_piece ptr = (TBEntry_piece)entry; //C++ TO C# CONVERTER TODO TASK: The memory management function 'free' has no equivalent in C#: free(ptr.precomp[0]); if (ptr.precomp[1] != null) { //C++ TO C# CONVERTER TODO TASK: The memory management function 'free' has no equivalent in C#: free(ptr.precomp[1]); } } else { TBEntry_pawn ptr = (TBEntry_pawn)entry; int f; for (f = 0; f < 4; f++) { //C++ TO C# CONVERTER TODO TASK: The memory management function 'free' has no equivalent in C#: free(ptr.file[f].precomp[0]); if (ptr.file[f].precomp[1] != null) { //C++ TO C# CONVERTER TODO TASK: The memory management function 'free' has no equivalent in C#: free(ptr.file[f].precomp[1]); } } } }
// determine file of leftmost pawn and sort pawns internal static int pawn_file(TBEntry_pawn ptr, int[] pos) { int i; for (i = 1; i < ptr.pawns[0]; i++) { if (flap[pos[0]] > flap[pos[i]]) { int tmp = pos[0]; pos[0] = pos[i]; pos[i] = tmp; } ; } return(file_to_file[pos[0] & 0x07]); }
internal static void set_norm_pawn(TBEntry_pawn ptr, byte[] norm, byte[] pieces) { int i; int j; for (i = 0; i < ptr.num; i++) { norm[i] = 0; } norm[0] = ptr.pawns[0]; if (ptr.pawns[1] != 0) { norm[ptr.pawns[0]] = ptr.pawns[1]; } for (i = ptr.pawns[0] + ptr.pawns[1]; i < ptr.num; i += norm[i]) { for (j = i; j < ptr.num && pieces[j] == pieces[i]; j++) { norm[i]++; } } }
internal static ulong encode_pawn(TBEntry_pawn ptr, byte[] norm, int[] pos, int[] factor) { ulong idx; int i; int j; int k; int m; int s; int t; int n = ptr.num; if (pos[0] & 0x04) { for (i = 0; i < n; i++) { pos[i] ^= 0x07; } } for (i = 1; i < ptr.pawns[0]; i++) { for (j = i + 1; j < ptr.pawns[0]; j++) { if (ptwist[pos[i]] < ptwist[pos[j]]) { int tmp = pos[i]; pos[i] = pos[j]; pos[j] = tmp; } ; } } t = ptr.pawns[0] - 1; idx = pawnidx[t, flap[pos[0]]]; for (i = t; i > 0; i--) { idx += binomial[t - i, ptwist[pos[i]]]; } idx *= factor[0]; // remaining pawns i = ptr.pawns[0]; t = i + ptr.pawns[1]; if (t > i) { for (j = i; j < t; j++) { for (k = j + 1; k < t; k++) { if (pos[j] > pos[k]) { int tmp = pos[j]; pos[j] = pos[k]; pos[k] = tmp; } ; } } s = 0; for (m = i; m < t; m++) { int p = pos[m]; for (k = 0, j = 0; k < i; k++) { j += (p > pos[k]); } s += binomial[m - i, p - j - 8]; } idx += ((ulong)s) * ((ulong)factor[i]); i = t; } for (; i < n;) { t = norm[i]; for (j = i; j < i + t; j++) { for (k = j + 1; k < i + t; k++) { if (pos[j] > pos[k]) { int tmp = pos[j]; pos[j] = pos[k]; pos[k] = tmp; } ; } } s = 0; for (m = i; m < i + t; m++) { int p = pos[m]; for (k = 0, j = 0; k < i; k++) { j += (p > pos[k]); } s += binomial[m - i, p - j]; } idx += ((ulong)s) * ((ulong)factor[i]); i += t; } return(idx); }
internal static void init_tb(ref string str) { FD fd = new FD(); TBEntry entry; int i; int j; int[] pcs = new int[16]; ulong key; ulong key2; int color; //C++ TO C# CONVERTER TODO TASK: Pointer arithmetic is detected on this variable, so pointers on this variable are left unchanged: sbyte *s; fd = GlobalMembersTbcore.open_tb(str, DefineConstants.WDLSUFFIX); if (fd == DefineConstants.FD_ERR) { return; } GlobalMembersTbcore.close_tb(fd); for (i = 0; i < 16; i++) { pcs[i] = 0; } color = 0; for (s = str; *s; s++) { switch (*s) { case 'P': pcs[DefineConstants.TB_PAWN | color]++; break; case 'N': pcs[DefineConstants.TB_KNIGHT | color]++; break; case 'B': pcs[DefineConstants.TB_BISHOP | color]++; break; case 'R': pcs[DefineConstants.TB_ROOK | color]++; break; case 'Q': pcs[DefineConstants.TB_QUEEN | color]++; break; case 'K': pcs[DefineConstants.TB_KING | color]++; break; case 'v': color = 0x08; break; } } for (i = 0; i < 8; i++) { if (pcs[i] != pcs[i + 8]) { break; } } key = GlobalMembersTbprobe.calc_key_from_pcs(pcs, 0); key2 = GlobalMembersTbprobe.calc_key_from_pcs(pcs, 1); if (pcs[DefineConstants.TB_PAWN] + pcs[DefineConstants.TB_PAWN | 8] == 0) { if (TBnum_piece == DefineConstants.TBMAX_PIECE) { Console.Write("TBMAX_PIECE limit too low!\n"); Environment.Exit(1); } entry = (TBEntry)TB_piece[TBnum_piece++]; } else { if (TBnum_pawn == DefineConstants.TBMAX_PAWN) { Console.Write("TBMAX_PAWN limit too low!\n"); Environment.Exit(1); } entry = (TBEntry)TB_pawn[TBnum_pawn++]; } entry.key = key; entry.ready = 0; entry.num = 0; for (i = 0; i < 16; i++) { entry.num += (byte)pcs[i]; } entry.symmetric = (key == key2); entry.has_pawns = (pcs[DefineConstants.TB_PAWN] + pcs[DefineConstants.TB_PAWN | 8] > 0); if (entry.num > GlobalMembersTbprobe.MaxCardinality) { GlobalMembersTbprobe.MaxCardinality = entry.num; } if (entry.has_pawns != 0) { TBEntry_pawn ptr = (TBEntry_pawn)entry; ptr.pawns[0] = (byte)pcs[DefineConstants.TB_PAWN]; ptr.pawns[1] = (byte)pcs[DefineConstants.TB_PAWN | 8]; if (pcs[DefineConstants.TB_PAWN | 8] > 0 && (pcs[DefineConstants.TB_PAWN] == 0 || pcs[DefineConstants.TB_PAWN | 8] < pcs[DefineConstants.TB_PAWN])) { ptr.pawns[0] = (byte)pcs[DefineConstants.TB_PAWN | 8]; ptr.pawns[1] = (byte)pcs[DefineConstants.TB_PAWN]; } } else { TBEntry_piece ptr = (TBEntry_piece)entry; for (i = 0, j = 0; i < 16; i++) { if (pcs[i] == 1) { j++; } } if (j >= 3) { ptr.enc_type = 0; } else if (j == 2) { ptr.enc_type = 2; } else // only for suicide { j = 16; for (i = 0; i < 16; i++) { if (pcs[i] < j && pcs[i] > 1) { j = pcs[i]; } ptr.enc_type = (byte)(1 + j); } } } GlobalMembersTbcore.add_to_hash(entry, key); if (key2 != key) { GlobalMembersTbcore.add_to_hash(entry, key2); } }
internal static int init_table_wdl(TBEntry entry, ref string str) { //C++ TO C# CONVERTER TODO TASK: C# does not have an equivalent for pointers to value types: //ORIGINAL LINE: byte *next; byte next; int f; int s; ulong[] tb_size = new ulong[8]; ulong[] size = new ulong[8 * 3]; byte flags; // first mmap the table into memory entry.data = GlobalMembersTbcore.map_file(str, DefineConstants.WDLSUFFIX, ref entry.mapping); if (entry.data == 0) { Console.Write("Could not find {0}" DefineConstants.WDLSUFFIX, str); return(0); } byte[] data = (byte)entry.data; if (data[0] != WDL_MAGIC[0] || data[1] != WDL_MAGIC[1] || data[2] != WDL_MAGIC[2] || data[3] != WDL_MAGIC[3]) { Console.Write("Corrupted table.\n"); GlobalMembersTbcore.unmap_file(ref entry.data, entry.mapping); entry.data = 0; return(0); } int split = data[4] & 0x01; int files = (data[4] & 0x02) != 0 ? 4 : 1; data += 5; if (entry.has_pawns == 0) { TBEntry_piece ptr = (TBEntry_piece)entry; GlobalMembersTbcore.setup_pieces_piece(ptr, data, tb_size[0]); data += ptr.num + 1; data += ((ushort)data) & 0x01; ptr.precomp[0] = GlobalMembersTbcore.setup_pairs(data, tb_size[0], size[0], next, ref flags, 1); data = next; if (split != 0) { ptr.precomp[1] = GlobalMembersTbcore.setup_pairs(data, tb_size[1], size[3], next, ref flags, 1); data = next; } else { ptr.precomp[1] = null; } ptr.precomp[0].indextable = (string)data; data += size[0]; if (split != 0) { ptr.precomp[1].indextable = (string)data; data += size[3]; } ptr.precomp[0].sizetable = (ushort)data; data += size[1]; if (split != 0) { ptr.precomp[1].sizetable = (ushort)data; data += size[4]; } data = (byte)((((ushort)data) + 0x3f) & ~0x3f); ptr.precomp[0].data = data; data += size[2]; if (split != 0) { data = (byte)((((ushort)data) + 0x3f) & ~0x3f); ptr.precomp[1].data = data; } } else { TBEntry_pawn ptr = (TBEntry_pawn)entry; s = 1 + (ptr.pawns[1] > 0); for (f = 0; f < 4; f++) { GlobalMembersTbcore.setup_pieces_pawn((TBEntry_pawn)ptr, data, tb_size[2 * f], f); data += ptr.num + s; } data += ((ushort)data) & 0x01; for (f = 0; f < files; f++) { ptr.file[f].precomp[0] = GlobalMembersTbcore.setup_pairs(data, tb_size[2 * f], size[6 * f], next, ref flags, 1); data = next; if (split != 0) { ptr.file[f].precomp[1] = GlobalMembersTbcore.setup_pairs(data, tb_size[2 * f + 1], size[6 * f + 3], next, ref flags, 1); data = next; } else { ptr.file[f].precomp[1] = null; } } for (f = 0; f < files; f++) { ptr.file[f].precomp[0].indextable = (string)data; data += size[6 * f]; if (split != 0) { ptr.file[f].precomp[1].indextable = (string)data; data += size[6 * f + 3]; } } for (f = 0; f < files; f++) { ptr.file[f].precomp[0].sizetable = (ushort)data; data += size[6 * f + 1]; if (split != 0) { ptr.file[f].precomp[1].sizetable = (ushort)data; data += size[6 * f + 4]; } } for (f = 0; f < files; f++) { data = (byte)((((ushort)data) + 0x3f) & ~0x3f); ptr.file[f].precomp[0].data = data; data += size[6 * f + 2]; if (split != 0) { data = (byte)((((ushort)data) + 0x3f) & ~0x3f); ptr.file[f].precomp[1].data = data; data += size[6 * f + 5]; } } } return(1); }
// probe_wdl_table and probe_dtz_table require similar adaptations. internal static int probe_wdl_table(Position pos, ref int success) { TBEntry ptr; TBHashEntry[] ptr2; ulong idx; ulong key; int i; byte res; int[] p = new int[DefineConstants.TBPIECES]; // Obtain the position's material signature key. key = pos.material_key(); // Test for KvK. if (key == (Zobrist.psq[(int)Color.WHITE][(int)PieceType.KING][0] ^ Zobrist.psq[(int)Color.BLACK][(int)PieceType.KING][0])) { return(0); } ptr2 = GlobalMembersTbcore.TB_hash[key >> (64 - DefineConstants.TBHASHBITS)]; for (i = 0; i < DefineConstants.HSHMAX; i++) { if (ptr2[i].key == key) { break; } } if (i == DefineConstants.HSHMAX) { success = 0; return(0); } ptr = ptr2[i].ptr; if (ptr.ready == 0) { LOCK(GlobalMembersTbcore.TB_mutex); if (ptr.ready == 0) { string str = new string(new char[16]); GlobalMembersTbprobe.prt_str(pos, str, ptr.key != key); if (GlobalMembersTbcore.init_table_wdl(ptr, ref str) == 0) { ptr2[i].key = 0UL; success = 0; UNLOCK(GlobalMembersTbcore.TB_mutex); return(0); } // Memory barrier to ensure ptr->ready = 1 is not reordered. #if _MSC_VER _ReadWriteBarrier(); #else __asm__ __volatile__ = new __asm__("" global::: "memory"); #endif ptr.ready = 1; } UNLOCK(GlobalMembersTbcore.TB_mutex); } int bside; int mirror; int cmirror; if (ptr.symmetric == 0) { if (key != ptr.key) { cmirror = 8; mirror = 0x38; bside = (pos.side_to_move() == Color.WHITE); } else { cmirror = mirror = 0; bside = !(pos.side_to_move() == Color.WHITE); } } else { cmirror = (int)pos.side_to_move() == ((int)Color.WHITE) != 0 ? 0 : 8; mirror = (int)pos.side_to_move() == ((int)Color.WHITE) != 0 ? 0 : 0x38; bside = 0; } // p[i] is to contain the square 0-63 (A1-H8) for a piece of type // pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king. // Pieces of the same type are guaranteed to be consecutive. if (ptr.has_pawns == 0) { TBEntry_piece entry = (TBEntry_piece)ptr; byte[] pc = entry.pieces[bside]; for (i = 0; i < entry.num;) { uint GlobalMembersBitboard.long bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), (PieceType)(pc[i] & 0x07)); do { p[i++] = (int)GlobalMembersBitboard.pop_lsb(ref bb); } while (bb); } idx = GlobalMembersTbcore.encode_piece(entry, entry.norm[bside], p, entry.factor[bside]); res = GlobalMembersTbcore.decompress_pairs(entry.precomp[bside], idx); } else { TBEntry_pawn entry = (TBEntry_pawn)ptr; int k = entry.file[0].pieces[0, 0] ^ cmirror; uint GlobalMembersBitboard.long bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07)); i = 0; do { p[i++] = (int)GlobalMembersBitboard.pop_lsb(ref bb) ^ mirror; } while (bb); int f = GlobalMembersTbcore.pawn_file(entry, p); byte[] pc = entry.file[f].pieces[bside]; for (; i < entry.num;) { bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), (PieceType)(pc[i] & 0x07)); do { p[i++] = (int)GlobalMembersBitboard.pop_lsb(ref bb) ^ mirror; } while (bb); } idx = GlobalMembersTbcore.encode_pawn(entry, entry.file[f].norm[bside], p, entry.file[f].factor[bside]); res = GlobalMembersTbcore.decompress_pairs(entry.file[f].precomp[bside], idx); } return(((int)res) - 2); }