UPDATE! - Unsafe Variations of FancyMagic and PEXT
Unsafe as per the C# spec where this is the name for raw pointer access which is uncommon in C#.
This should also be a first for C# (in terms of performance and variety) and now yields these results:
Code: Select all
AMD Ryzen 9 5950X 16-Core Processor
Verify Engines...OK
Algorithm Million Queen/s
CSharp Native Code
Switch 26,47
ObstructionDiff 183,22
Leorik 183,49
HyperbolaQsc 237,71
FancyMagic 260,60
FancyMagic Unsafe 305,44
Pext 249,18
Pext Unsafe 311,25
The results are very reproducable (+-3%)
So each unsafe variant is a good 20% faster than the safe code - but still less than half the speed of native C++.
I want to reiterate that speed is not a metric of the language itself but a metric for the compiler.
In fact C# could be faster than C++ since runtime configuration in JIT could become a constexpr for the compiler. C++ ahead of time compilers cannot infer what a uci option will be for example. A JIT language could compile the changed methods on the fly with march=native and turn variables into inline x86 constants. Another example: A program launch configfile that set something to zero or false. All the disabled codeparts would be zero cost.
Unsafe in C# allows us to remove one addition that usually is needed:
Code: Select all
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ulong Rook(int sq, ulong occupy)
{
return Entries[sq].atk_rook[Bmi2.X64.ParallelBitExtract(occupy, Entries[sq].mask_rook)];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Bishop(int sq, ulong occupy)
{
return Attacks[Entries[sq].offset_bish + Bmi2.X64.ParallelBitExtract(occupy, Entries[sq].mask_bish)];
}