Page 1 of 4

Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 11:58 am
by vittyvirus
Stockfish is doesn't seem to find hardware instructions on MSVC++ 32-bit. 32-bit MSVC++ doesn't have 64-bit functions like _BitScanForward64().
So, instead of:

Code: Select all

#ifdef USE_BSFQ

#  if defined(_MSC_VER) && !defined(__INTEL_COMPILER)

FORCE_INLINE Square lsb(Bitboard b) {
  unsigned long idx;
  _BitScanForward64(&idx, b);
  return (Square) idx;
}

FORCE_INLINE Square msb(Bitboard b) {
  unsigned long idx;
  _BitScanReverse64(&idx, b);
  return (Square) idx;
}
...
they should've done:

Code: Select all

#  if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#ifdef _WIN64
FORCE_INLINE Square lsb(Bitboard b) {
  unsigned long idx;
  _BitScanForward64(&idx, b);
  return (Square) idx;
}

FORCE_INLINE Square msb(Bitboard b) {
  unsigned long idx;
  _BitScanReverse64(&idx, b);
  return (Square) idx;
}
#else
FORCE_INLINE Square lsb(Bitboard b) {
	unsigned long idx;
	if (unsigned (b))
	{
		_BitScanForward(&idx, b);
		return Square (idx);
	} else
	{
		_BitScanForward(&idx, (b >> 32));
		return Square(idx + 32);
	}
  /*unsigned long idx;
  _BitScanForward64(&idx, b);
  return (Square) idx;*/
}

FORCE_INLINE Square msb(Bitboard b) {
	unsigned long idx, b32 = (b >> 32);
	if (b32)
	{
		_BitScanReverse(&idx, b32);
		return Square(idx + 32);
	} else
	{
		_BitScanReverse(&idx, unsigned (b));
		return Square(idx);
	}
  /*unsigned long idx;
  _BitScanReverse64(&idx, b);
  return (Square) idx;*/
}
#endif
Similar for popcnt.

Code: Select all

...
#elif defined(_MSC_VER) && defined(__INTEL_COMPILER) // Almost never true

  return _mm_popcnt_u64(b);

#elif defined(_MSC_VER)
	unsigned lo = b, hi = (b >> 32);
	return _mm_popcnt_u32(lo) + _mm_popcnt_u32(hi);
  //return (int) __popcnt64(b);

#else
...
The solution that worked for me was:

Code: Select all

...
#elif defined(_MSC_VER)
#ifdef _WIN64
  return _mm_popcnt_u64(b);
#else
	unsigned lo = b, hi = (b >> 32);
	return _mm_popcnt_u32(lo) + _mm_popcnt_u32(hi);
#endif
#else
...
I hope the SF team considers this.

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 3:14 pm
by zamar
Thanks Syed,

I've raised a new issue about this:
https://github.com/official-stockfish/S ... issues/178

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 3:37 pm
by vittyvirus
zamar wrote:Thanks Syed,

I've raised a new issue about this:
https://github.com/official-stockfish/S ... issues/178
Thank you for raising that issue! It's my pleasure to make a slight contribution to such a legendary and clean coded engine, which I myself love!

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 5:34 pm
by Damir
Hi Syed

Maybe you can contribute even more, by helping the Stockfish team improve their engine.. Commercial engines like Komodo need to be knocked out in the next TCEC season. I am sure that SF team would greatly appreciate all the help you can give them, since they seem to be stucked in improving their engine.

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 6:33 pm
by vittyvirus
Damir wrote:Hi Syed

Maybe you can contribute even more, by helping the Stockfish team improve their engine.. Commercial engines like Komodo need to be knocked out in the next TCEC season. I am sure that SF team would greatly appreciate all the help you can give them, since they seem to be stucked in improving their engine.
I love Stockfish. So if I would ever be able to join Stockfish team, it'll be my pleasure. And yes, it hurts to see that SF lost TCEC.
But I don't know where I should start. I mean, I have heard but technically don't know any Stockfish weaknesses. If anyone has an idea, then I can surely work on it...

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 6:52 pm
by Damir
Komodo won the match because it got more wins in closed positions. Stockfish main weakness is it does not know how to play closed games Komodo clearly outplayed SF in closed positions. If you can improve Stockfish in how to play better closed games, it might be able to beat Komodo in next TCEC season... Just my 2 cents

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 7:03 pm
by vittyvirus
Damir wrote:Komodo won the match because it got more wins in closed positions. Stockfish main weakness is it does not know how to play closed games Komodo clearly outplayed SF in closed positions. If you can improve Stockfish in how to play better closed games, it might be able to beat Komodo in next TCEC season... Just my 2 cents
Really? I'd look into the matter. Also, Stockfish seems ro be stupid in some endgames, but much less than other engines.
In closed positions, this might be caused by over-pruning..

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 8:00 pm
by Damir
If you can manage to improve Stockfish play better in closed positions, you will manage to secure its victory against Komodo in the next TCEC event...

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Tue Dec 30, 2014 8:18 pm
by vittyvirus
Damir wrote:If you can manage to improve Stockfish play better in closed positions, you will manage to secure its victory against Komodo in the next TCEC event...
...and in Worpd Computer Chess Championship.
But the bottomside is, the Stockfish maintainers and team knows how difficult it is to do...

Re: Stockfish 32-bit and hardware instructions on MSVC++

Posted: Wed Dec 31, 2014 9:37 am
by mcostalba
Syed, what command line you use to compile?

You have that error because you force -DUSE_BSFQ somehow, while you shouldn't