Looking for intrinsic "least significant bit" on Visual Studio

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

mar
Posts: 2566
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by mar »

I did some googling and you you want is:

Code: Select all

#include <immintrin.h>
then use

Code: Select all

_tzcnt_u64(x)
_lzcnt_u64(x)
note that those are BMI1 (actually lzcnt was orginally ABM and tzcnt was added later as part of BMI1 as far as I understand)
chrisw
Posts: 4345
Joined: Tue Apr 03, 2012 4:28 pm

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by chrisw »

OliverBr wrote: Thu Sep 03, 2020 6:21 pm
chrisw wrote: Thu Sep 03, 2020 5:56 pm If I remember correct from 1997, we were using beta dev version of what became Visual Studio and CSTal had the asm instruction in some sort of macro. I’ll look up what I do now when I get home, but am fairly sure there’s a standard fast way that works, may well be the Mar instructions above, and I do think you indeed have to check you’re giving them non zero data.
An asm code snippet for Visual Studio would be very helpful. This is the machine command:
https://www.felixcloutier.com/x86/tzcnt

(the parameter is never 0)
long time ago, and a support programmer wrote that stuff, but I think there is or was a pragma that allowed asm, and magically parameters were passed in registers, but that would need looking up. Quick search mobile phone suggests nowadays there’s an __asm or _asm to allow an asm block in VS, but I didn’t immediately find how you pass data in and out. Would be interested in what you find.
Btw, are you compiling SF NNUE in VS, I got stuck on compile errors demanding c++17 support?
No, I am developing OliThink572 using "tzcnt" and looking for a solution for my Windows executables. _BitscanForward64 works, but is slower.
mar
Posts: 2566
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by mar »

chrisw wrote: Thu Sep 03, 2020 6:46 pm long time ago, and a support programmer wrote that stuff, but I think there is or was a pragma that allowed asm, and magically parameters were passed in registers, but that would need looking up. Quick search mobile phone suggests nowadays there’s an __asm or _asm to allow an asm block in VS, but I didn’t immediately find how you pass data in and out. Would be interested in what you find.
unfortunately, inline assembly is only supported in 32-bit mode by microsoft compiler (in 64-bit mode you either have to use intrinsics or use masm to compile asm file, which in this case is out of question as that would mean a function call)
OliverBr
Posts: 725
Joined: Tue Dec 18, 2007 9:38 pm
Location: Munich, Germany
Full name: Dr. Oliver Brausch

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by OliverBr »

mar wrote: Thu Sep 03, 2020 6:45 pm I did some googling and you you want is:

Code: Select all

#include <immintrin.h>
then use

Code: Select all

_tzcnt_u64(x)
_lzcnt_u64(x)
note that those are BMI1 (actually lzcnt was orginally ABM and tzcnt was added later as part of BMI1 as far as I understand)
Thank you. To already existing includes

Code: Select all

#include <windows.h>
#include <conio.h>
...
_lzcnt_u64(x)
does the job. I didn't need to include explicitly immintrin.h, but I think VS2019 is doing it implicitly.
It works perfectly.

_BitScanForward worked, too, but is slower. Both are clearly faster than own implemented methods.
Chess Engine OliThink: http://brausch.org/home/chess
OliThink GitHub:https://github.com/olithink
Dann Corbit
Posts: 12550
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by Dann Corbit »

It is better to include the header.
For Visual C++ here is a list for x86:
https://docs.microsoft.com/en-us/cpp/in ... ew=vs-2019
And here is a list for x64:
https://docs.microsoft.com/en-us/cpp/in ... ew=vs-2019

Also, use the following sort of pragma for each intrinsic you use:
#pragma intrinsic(_BitScanReverse)

Otherwise you might do a function call instead of inline.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
Dann Corbit
Posts: 12550
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by Dann Corbit »

From: https://docs.microsoft.com/en-us/cpp/pr ... ew=vs-2019
we have this:
"Programs that use intrinsic functions are faster because they don't have the overhead of function calls. However, they may be larger because of the additional code generated."
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
OliverBr
Posts: 725
Joined: Tue Dec 18, 2007 9:38 pm
Location: Munich, Germany
Full name: Dr. Oliver Brausch

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by OliverBr »

I included the header and released OliThink 5.7.2. Everything is smaller now, code size (45k, 1615 lines), executable size (90kb), memory/cache usage, time usage.

ELO boost is +19 on a Linux AMD EPYC 7502P:

Code: Select all

   # PLAYER             :  RATING  ERROR  POINTS  PLAYED   (%)     W     D     L  D(%)  CFS(%)
   1 OliThink 5.7.2     :      19      5  5277.0   10000  52.8  3152  4250  2598  42.5     100
   2 OliThink 5.7.1     :       0   ----  4723.0   10000  47.2  2598  4250  3152  42.5     ---

White advantage = -3.02 +/- 2.69
Draw rate (equal opponents) = 42.62 % +/- 0.50
Elo difference: 19.27 +/- 5.16
On my Mac it's even over 30 (I guess this is because the cpu has much less cache and I could remove a lot of cache pressure).

Here is a mini tourney against friends.

Code: Select all

   # PLAYER                   :  RATING  ERROR  POINTS  PLAYED   (%)    W    D    L  D(%)  CFS(%)
   1 Fruit 2.1                :      37     29   276.0     500  55.2  230   92  178  18.4      57
   2 Scorpio_2.8.8 MCTS+NN    :      33     28   273.5     500  54.7  236   75  189  15.0      91
   3 K2 v.087                 :       4     29   252.5     500  50.5  205   95  200  19.0      60
   4 OliThink 5.7.2           :       0   ----  1008.0    2000  50.4  837  342  821  17.1     100
   5 Arasan 11.7              :     -86     28   190.0     500  38.0  150   80  270  16.0     ---

White advantage = 0.00 +/- 7.21
Draw rate (equal opponents) = 17.30 % +/- 0.85
The first time even ever OliThink won more games than lost in this round.
Chess Engine OliThink: http://brausch.org/home/chess
OliThink GitHub:https://github.com/olithink
Dann Corbit
Posts: 12550
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by Dann Corbit »

OliverBr wrote: Thu Sep 03, 2020 9:53 pm I included the header and released OliThink 5.7.2. Everything is smaller now, code size (45k, 1615 lines), executable size (90kb), memory/cache usage, time usage.
Here is a mini tourney against friends.

Code: Select all

   # PLAYER                   :  RATING  ERROR  POINTS  PLAYED   (%)    W    D    L  D(%)  CFS(%)
   1 Fruit 2.1                :      37     29   276.0     500  55.2  230   92  178  18.4      57
   2 Scorpio_2.8.8 MCTS+NN    :      33     28   273.5     500  54.7  236   75  189  15.0      91
   3 K2 v.087                 :       4     29   252.5     500  50.5  205   95  200  19.0      60
   4 OliThink 5.7.2           :       0   ----  1008.0    2000  50.4  837  342  821  17.1     100
   5 Arasan 11.7              :     -86     28   190.0     500  38.0  150   80  270  16.0     ---
The first time even ever OliThink won more games than lost in this round.
Looks like you are about to scale K2
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by Bo Persson »

Dann Corbit wrote: Thu Sep 03, 2020 7:35 pm From: https://docs.microsoft.com/en-us/cpp/pr ... ew=vs-2019
we have this:
"Programs that use intrinsic functions are faster because they don't have the overhead of function calls. However, they may be larger because of the additional code generated."
But, of course, when the intrinsic is a single machine instrution the result is a program that is both smaller and faster. :)
MOBMAT
Posts: 390
Joined: Sat Feb 04, 2017 11:57 pm
Location: USA

Re: Looking for intrinsic "least significant bit" on Visual Studio

Post by MOBMAT »

This shows all the options...

https://en.wikipedia.org/wiki/Find_first_set
i7-6700K @ 4.00Ghz 32Gb, Win 10 Home, EGTBs on PCI SSD
Benchmark: Stockfish15.1 NNUE x64 bmi2 (nps): 1277K