For gcc/clang both intrinsic functions are available for most and least significant bit (leading/trailing zeros):
Code: Select all
__builtin_clz
__builtin_ctz
Code: Select all
__lzcnt64
Is there a function to get LSB in Visual Studio?
Moderators: hgm, Rebel, chrisw
Code: Select all
__builtin_clz
__builtin_ctz
Code: Select all
__lzcnt64
I fear you haven't got my question correctly.
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.mar wrote: ↑Thu Sep 03, 2020 4:52 pm this is not exactly the same but you may be looking for _BitscanForward64 and _BitscanReverse64, which translate to bsf/bsr x86 instructions
I believe __lzcnt64 is available on relatively new machines only
bsf/bsr both return the index of the least (forward) or most (reverse) bit, but is undefined for zero, so you should explicitly check for 0 yourself
Thank you. _BitscanForward64 helps me a bit, even it is not as fast as __lzcnt64. (< 10% gain)mar wrote: ↑Thu Sep 03, 2020 4:52 pm this is not exactly the same but you may be looking for _BitscanForward64 and _BitscanReverse64, which translate to bsf/bsr x86 instructions
I believe __lzcnt64 is available on relatively new machines only
bsf/bsr both return the index of the least (forward) or most (reverse) bit, but is undefined for zero, so you should explicitly check for 0 yourself
An asm code snippet for Visual Studio would be very helpful. This is the machine command: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.
No, I am developing OliThink572 using "tzcnt" and looking for a solution for my Windows executables. _BitscanForward64 works, but is slower.Btw, are you compiling SF NNUE in VS, I got stuck on compile errors demanding c++17 support?
Maybe this helpsOliverBr wrote: ↑Thu Sep 03, 2020 3:43 pm Hello,
For gcc/clang both intrinsic functions are available for most and least significant bit (leading/trailing zeros):For Visual Studio 2019 I have found an intrinsic function for most significant bit (leading zeros)Code: Select all
__builtin_clz __builtin_ctz
A function named "__tzcnt64" doesn't exist. (trailing zeros)Code: Select all
__lzcnt64
Is there a function to get LSB in Visual Studio?
The reason might be that AMD's Advanced Bit Manipulation had only _lzcnt64, and trailing zero count came later with Intel's BMI1.OliverBr wrote: ↑Thu Sep 03, 2020 6:15 pm
I still don't get, why there is no "__tzcnt64" in Visual Studio, as the processors that support "lzcnt" do automatically support "tzcnt".
See also:
https://www.felixcloutier.com/x86/lzcnt
https://www.felixcloutier.com/x86/tzcnt
Again, gcc/clang do support both commands. I get great results in Linux and MacOSX. (> 20% gain)