Bitboards ?: C# vs C++

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Bitboards ?: C# vs C++

Post by Chessnut1071 »

This is my 1st attempt to convert to bitboards from pre computed byte tables. I noticed that most are programmed in C++. I just got used to C# from VB,Net. The procedure I'm looking at uses [bit scan forward] for move generation. Is there a C# equivalent for this C++ command? I can't believe I'm wasting months on converting to a language I used to laugh at a year ago. Go figure.
Luecx
Posts: 138
Joined: Thu Jun 18, 2020 9:20 pm
Full name: Finn Eggers

Re: Bitboards ?: C# vs C++

Post by Luecx »

Chessnut1071 wrote: Wed Nov 17, 2021 6:45 am This is my 1st attempt to convert to bitboards from pre computed byte tables. I noticed that most are programmed in C++. I just got used to C# from VB,Net. The procedure I'm looking at uses [bit scan forward] for move generation. Is there a C# equivalent for this C++ command? I can't believe I'm wasting months on converting to a language I used to laugh at a year ago. Go figure.

Code: Select all

ulong value = 18;
ulong result = System.Runtime.Intrinsics.X86.Bmi1.X64.TrailingZeroCount(value);
Took me 12 seconds to google.
The ability to speak does not make you intelligent. https://github.com/Luecx/Koivisto

Image
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Bitboards ?: C# vs C++

Post by dangi12012 »

Chessnut1071 wrote: Wed Nov 17, 2021 6:45 am This is my 1st attempt to convert to bitboards from pre computed byte tables. I noticed that most are programmed in C++. I just got used to C# from VB,Net. The procedure I'm looking at uses [bit scan forward] for move generation. Is there a C# equivalent for this C++ command? I can't believe I'm wasting months on converting to a language I used to laugh at a year ago. Go figure.
If you are still stuck on dotnet 4.8 then you can use numerics nuget package.
For .net core and .net 5 you have the answer above.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Bitboards ?: C# vs C++

Post by Chessnut1071 »

Luecx wrote: Wed Nov 17, 2021 6:54 am
Chessnut1071 wrote: Wed Nov 17, 2021 6:45 am This is my 1st attempt to convert to bitboards from pre computed byte tables. I noticed that most are programmed in C++. I just got used to C# from VB,Net. The procedure I'm looking at uses [bit scan forward] for move generation. Is there a C# equivalent for this C++ command? I can't believe I'm wasting months on converting to a language I used to laugh at a year ago. Go figure.

Code: Select all

ulong value = 18;
ulong result = System.Runtime.Intrinsics.X86.Bmi1.X64.TrailingZeroCount(value);
Took me 12 seconds to google.
Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Bitboards ?: C# vs C++

Post by Chessnut1071 »

Luecx wrote: Wed Nov 17, 2021 6:54 am
Chessnut1071 wrote: Wed Nov 17, 2021 6:45 am This is my 1st attempt to convert to bitboards from pre computed byte tables. I noticed that most are programmed in C++. I just got used to C# from VB,Net. The procedure I'm looking at uses [bit scan forward] for move generation. Is there a C# equivalent for this C++ command? I can't believe I'm wasting months on converting to a language I used to laugh at a year ago. Go figure.

Code: Select all

ulong value = 18;
ulong result = System.Runtime.Intrinsics.X86.Bmi1.X64.TrailingZeroCount(value);
Took me 12 seconds to google.
Something's missing?
error CS0234: The type or namespace name 'Intrinsics' does not exist in the namespace 'System.Runtime' (are you missing an assembly reference?)
User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Bitboards ?: C# vs C++

Post by emadsen »

Chessnut1071 wrote: Wed Nov 17, 2021 6:45 am This is my 1st attempt to convert to bitboards from pre computed byte tables. I noticed that most are programmed in C++. I just got used to C# from VB,Net. The procedure I'm looking at uses [bit scan forward] for move generation. Is there a C# equivalent for this C++ command? I can't believe I'm wasting months on converting to a language I used to laugh at a year ago. Go figure.
I use System.Numerics.BitOperations.TrailingZeroCount and BitOperations.PopCount for .NET 6. I'm not sure when that namespace was added. Perhaps for .NET 5? I use preprocessor directives to fall back to a de Bruijn sequence if bit scan intrinsics aren't available. This produces a separate EXE via build configuration.
My C# chess engine: https://www.madchess.net
User avatar
emadsen
Posts: 434
Joined: Thu Apr 26, 2012 1:51 am
Location: Oak Park, IL, USA
Full name: Erik Madsen

Re: Bitboards ?: C# vs C++

Post by emadsen »

I just realized System.Numerics.BitOperations class "provides utility methods for intrinsic bit-twiddling operations. The methods use hardware intrinsics when available on the underlying platform; otherwise, they use optimized software fallbacks." See the doc. So no need for preprocessor directives, assuming Microsoft's software fallback code performs well.

I must have introduced the preprocessor directives when I was using System.Runtime.Intrinsics.X86 prior to the availability of System.Numerics.BitOperations.
My C# chess engine: https://www.madchess.net
Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Bitboards ?: C# vs C++

Post by Chessnut1071 »

emadsen wrote: Wed Nov 17, 2021 5:45 pm I just realized System.Numerics.BitOperations class "provides utility methods for intrinsic bit-twiddling operations. The methods use hardware intrinsics when available on the underlying platform; otherwise, they use optimized software fallbacks." See the doc. So no need for preprocessor directives, assuming Microsoft's software fallback code performs well.

I must have introduced the preprocessor directives when I was using System.Runtime.Intrinsics.X86 prior to the availability of System.Numerics.BitOperations.
Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Bitboards ?: C# vs C++

Post by Chessnut1071 »

emadsen wrote: Wed Nov 17, 2021 5:45 pm I just realized System.Numerics.BitOperations class "provides utility methods for intrinsic bit-twiddling operations. The methods use hardware intrinsics when available on the underlying platform; otherwise, they use optimized software fallbacks." See the doc. So no need for preprocessor directives, assuming Microsoft's software fallback code performs well.

I must have introduced the preprocessor directives when I was using System.Runtime.Intrinsics.X86 prior to the availability of System.Numerics.BitOperations.
I think a lot of those numeric bit operations were put in to satisfy the chess programming community once bit boards were introduced. I didn't realize the vast number of programmers hooked on this huge time sink. It really takes you to the edge on programming techniques though.
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Bitboards ?: C# vs C++

Post by dangi12012 »

Chessnut1071 wrote: Wed Nov 17, 2021 7:05 pm
emadsen wrote: Wed Nov 17, 2021 5:45 pm I just realized System.Numerics.BitOperations class "provides utility methods for intrinsic bit-twiddling operations. The methods use hardware intrinsics when available on the underlying platform; otherwise, they use optimized software fallbacks." See the doc. So no need for preprocessor directives, assuming Microsoft's software fallback code performs well.

I must have introduced the preprocessor directives when I was using System.Runtime.Intrinsics.X86 prior to the availability of System.Numerics.BitOperations.
I think a lot of those numeric bit operations were put in to satisfy the chess programming community once bit boards were introduced. I didn't realize the vast number of programmers hooked on this huge time sink. It really takes you to the edge on programming techniques though.
Nah we chessprogrammers get the scraps of cryptography and AI learning and compression instructions.
If we would get an instruction it would be one instruction to get all sliding piece attacks for the board in one cycle.
But compression gifted us with PEXT and thats the fastest slider lookup currently.

Anyways my very first movegen was in C# and the naive approach with bijective dictionaries. (Attacked squares and Attacked by list for each square)
and got 3MNps.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer