Computing hash "on the fly"

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Computing hash "on the fly"

Post by stegemma »

AlvaroBegue wrote:

Code: Select all

            HH(boKings);
            HH(boQueens);
            HH(boRooks);
            HH(boBishops);
            HH(boKnights);
            HH(boPawns);
That can probably just be substituted with something like this:

Code: Select all

            HH(boKings|boRooks|boKnights);
            HH(boQueens|boRooks);
            HH(boBishops|boKnights);
This should be faster. It is making use of the fact that a square will only contain one type of piece, so you can assign each piece a 3-bit code and compute a hash of that more compact representation.
It could work but it seems to have more false positive TT hits. I've tested various solutions and the first one it is the only with no flase positive and just a little slower than Zobrist:

Code: Select all

	uint64_t clsBoard::GetHash()
	{
		#if FAST_TT
			#define HH(a) seeds[0] ^= (a); boHash ^= uRandS(seeds[0], seeds[1]);
			uint64_t seeds[]  = { 0xA10E014701010401ULL, 0x02C070202CDE2ULL };
			static const uint64_t colors[] = { 0x1C78B99395A740FC, 0x791C65D25F6AD945 };
			#define FAST_TT_TEST 1
			#if FAST_TT_TEST==1
				boHash = boAllPieces;
				HH(boKings)
				HH(boQueens)
				HH(boRooks)
				HH(boBishops)
				HH(boKnights)
				HH(boPawns)
				HH(color)
				HH(boFriends)
				HH(boEnpPawn - boCastlings)
			#elif FAST_TT_TEST==2
				boHash = boAllPieces;
				HH(boKings | boQueens)
				HH(boQueens | boRooks)
				HH(boRooks | boBishops)
				HH(boBishops | boKnights)
				HH(boPawns | color)
				HH(boFriends)
				HH(boEnpPawn - boCastlings)
			#elif FAST_TT_TEST==3
				boHash = boAllPieces;
				HH(boKings | boRooks | boKnights);
				HH(boQueens | boRooks);
				HH(boBishops | boKnights);
				HH(boPawns | color)
				HH(boFriends)
				HH(boEnpPawn - boCastlings)
			#elif FAST_TT_TEST==4
			  // cba
			  // 001 boKings
			  // 010 boQueens
			  // 011 boRooks
			  // 100 boBishops
			  // 101 boKnights
			  // 110 boPawns
				boHash = boAllPieces;
				HH(boKings   | boRooks   | boKnights )  // a
				HH(boQueens  | boRooks   | boPawns   )  // b
				HH(boBishops | boKnights | boPawns   )  // c
				HH(color)
				HH(boFriends)
				HH(boEnpPawn - boCastlings)
			#endif
			return boHash;
		#else 
			return boHash ^ boAllPieces ^ (boEnpPawn - boCastlings) ^ zobcol[color];
		#endif 
	}
In this tests, TT based pruning has been disabled and I haven't quiescence, null moves and so on.
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com