Bitboards ?: C# vs C++
Moderators: hgm, Rebel, chrisw
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Bitboards ?: C# vs C++
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.
-
- Posts: 138
- Joined: Thu Jun 18, 2020 9:20 pm
- Full name: Finn Eggers
Re: Bitboards ?: C# vs C++
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);
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Bitboards ?: C# vs C++
If you are still stuck on dotnet 4.8 then you can use numerics nuget package.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.
For .net core and .net 5 you have the answer above.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Re: Bitboards ?: C# vs C++
Luecx wrote: ↑Wed Nov 17, 2021 6:54 amChessnut1071 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.Took me 12 seconds to google.Code: Select all
ulong value = 18; ulong result = System.Runtime.Intrinsics.X86.Bmi1.X64.TrailingZeroCount(value);
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Re: Bitboards ?: C# vs C++
Something's missing?Luecx wrote: ↑Wed Nov 17, 2021 6:54 amChessnut1071 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.Took me 12 seconds to google.Code: Select all
ulong value = 18; ulong result = System.Runtime.Intrinsics.X86.Bmi1.X64.TrailingZeroCount(value);
error CS0234: The type or namespace name 'Intrinsics' does not exist in the namespace 'System.Runtime' (are you missing an assembly reference?)
-
- Posts: 434
- Joined: Thu Apr 26, 2012 1:51 am
- Location: Oak Park, IL, USA
- Full name: Erik Madsen
Re: Bitboards ?: C# vs C++
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.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.
My C# chess engine: https://www.madchess.net
-
- Posts: 434
- Joined: Thu Apr 26, 2012 1:51 am
- Location: Oak Park, IL, USA
- Full name: Erik Madsen
Re: Bitboards ?: C# vs C++
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 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
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Re: Bitboards ?: C# vs C++
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.
-
- Posts: 313
- Joined: Tue Aug 03, 2021 2:41 pm
- Full name: Bill Beame
Re: Bitboards ?: C# vs C++
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.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.
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Bitboards ?: C# vs C++
Nah we chessprogrammers get the scraps of cryptography and AI learning and compression instructions.Chessnut1071 wrote: ↑Wed Nov 17, 2021 7:05 pmI 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.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.
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
Daniel Inführ - Software Developer