Created
December 15, 2023 07:39
-
-
Save stolnikov/201677aa431c2b0ca6f41727adc169b3 to your computer and use it in GitHub Desktop.
Odin vs C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Runtime.InteropServices; | |
using System.Runtime.Intrinsics; | |
namespace CSharpLib; | |
public class CSharpBench | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static int[] CSharp_Bench2(int[] a, int[] b, int[] result, int size) | |
{ | |
int vectorSize = Vector256<int>.Count; | |
var spanA = a.AsSpan(); | |
var spanB = b.AsSpan(); | |
var spanResult = MemoryMarshal.Cast<int, Vector256<int>>(result); | |
var vectorsA = MemoryMarshal.Cast<int, Vector256<int>>(spanA); | |
var vectorsB = MemoryMarshal.Cast<int, Vector256<int>>(spanB); | |
for (var i = 0; i < vectorsA.Length; i++) | |
{ | |
var va = vectorsA[i]; | |
var vb = vectorsB[i]; | |
var mask = Vector256.LessThan(va, vb); | |
var vsum = Vector256.Add(va, Vector256.Multiply(vb, va)); | |
var vdiff = Vector256.Subtract(Vector256.Multiply(va, vb), vb); | |
var vresult = Vector256.ConditionalSelect(mask, vsum, vdiff); | |
spanResult[i] = vresult; | |
} | |
var remaining = size - spanA.Length * vectorSize; | |
for (int i = size - remaining; i < size; i++) | |
{ | |
if (a[i] < b[i]) | |
{ | |
result[i] = a[i] + b[i]; | |
} | |
else | |
{ | |
result[i] = a[i] - b[i]; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References: