Stockfish 32-bit and hardware instructions on MSVC++

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

User avatar
vittyvirus
Posts: 646
Joined: Wed Jun 18, 2014 2:30 pm
Full name: Fahad Syed

Stockfish 32-bit and hardware instructions on MSVC++

Post 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.
zamar
Posts: 613
Joined: Sun Jan 18, 2009 7:03 am

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

Post by zamar »

Thanks Syed,

I've raised a new issue about this:
https://github.com/official-stockfish/S ... issues/178
Joona Kiiski
User avatar
vittyvirus
Posts: 646
Joined: Wed Jun 18, 2014 2:30 pm
Full name: Fahad Syed

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

Post 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!
Damir
Posts: 2801
Joined: Mon Feb 11, 2008 3:53 pm
Location: Denmark
Full name: Damir Desevac

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

Post 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.
User avatar
vittyvirus
Posts: 646
Joined: Wed Jun 18, 2014 2:30 pm
Full name: Fahad Syed

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

Post 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...
Damir
Posts: 2801
Joined: Mon Feb 11, 2008 3:53 pm
Location: Denmark
Full name: Damir Desevac

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

Post 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
User avatar
vittyvirus
Posts: 646
Joined: Wed Jun 18, 2014 2:30 pm
Full name: Fahad Syed

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

Post 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..
Damir
Posts: 2801
Joined: Mon Feb 11, 2008 3:53 pm
Location: Denmark
Full name: Damir Desevac

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

Post 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...
User avatar
vittyvirus
Posts: 646
Joined: Wed Jun 18, 2014 2:30 pm
Full name: Fahad Syed

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

Post 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...
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

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

Post by mcostalba »

Syed, what command line you use to compile?

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