using System.Diagnostics;
using System.Threading.Tasks;
public static void Main()
Console.WriteLine("Hello");
Console.WriteLine($"Vector<byte>.IsSupported = {Vector<byte>.IsSupported}");
Console.WriteLine($"Vector<sbyte>.IsSupported = {Vector<sbyte>.IsSupported}");
Console.WriteLine($"Vector<short>.IsSupported = {Vector<short>.IsSupported}");
Console.WriteLine($"Vector<ushort>.IsSupported = {Vector<ushort>.IsSupported}");
Console.WriteLine($"Vector<int>.IsSupported = {Vector<int>.IsSupported}");
Console.WriteLine($"Vector<uint>.IsSupported = {Vector<uint>.IsSupported}");
Console.WriteLine($"Vector<long>.IsSupported = {Vector<long>.IsSupported}");
Console.WriteLine($"Vector<ulong>.IsSupported = {Vector<ulong>.IsSupported}");
Console.WriteLine($"Vector<Int128>.IsSupported = {Vector<Int128>.IsSupported}");
Console.WriteLine($"Vector<UInt128>.IsSupported = {Vector<UInt128>.IsSupported}");
Console.WriteLine($"Vector<nint>.IsSupported = {Vector<nint>.IsSupported}");
Console.WriteLine($"Vector<nuint>.IsSupported = {Vector<nuint>.IsSupported}");
Console.WriteLine($"nint.Size = {nint.Size}");
Console.WriteLine($"Is64BitOperatingSystem = {Environment.Is64BitOperatingSystem}");
Console.WriteLine($"Is64BitProcess = {Environment.Is64BitProcess}");
Console.WriteLine($"ProcessorCount = {Environment.ProcessorCount}");
Console.WriteLine($"OSVersion = {Environment.OSVersion}");
Console.WriteLine($".NET Version = {Environment.Version}");
Console.WriteLine($"Vector<long>.IsSupported = {Vector<long>.IsSupported}");
Console.WriteLine($"Vector.IsHardwareAccelerated = {Vector.IsHardwareAccelerated}");
Console.WriteLine($"Vector<long>.Count = {Vector<long>.Count}");
var array = new long[1_600_000];
var random = new Random();
for(var i = 0; i < array.Length; i++)
array[i] = random.Next(100);
for(var r = 0; r < repeat; r++)
Stopwatch stopwatch = new Stopwatch();
for(var r = 0; r < repeat; r++)
Console.WriteLine($"Sum = {sum} for {stopwatch.ElapsedTicks:n0} ticks");
for(var r = 0; r < repeat; r++)
Stopwatch stopwatch = new Stopwatch();
for(var r = 0; r < repeat; r++)
Console.WriteLine($"SumSimd = {sum} for {stopwatch.ElapsedTicks:n0} ticks");
for(var r = 0; r < repeat; r++)
sum = SumSimdParallel(array);
Stopwatch stopwatch = new Stopwatch();
for(var r = 0; r < repeat; r++)
sum = SumSimdParallel(array);
Console.WriteLine($"SumSimdParallel = {sum} for {stopwatch.ElapsedTicks:n0} ticks");
Console.WriteLine("Goodbye");
public static long SumSimd(long[] array)
var vectorSize = Vector<long>.Count;
var vectorSum = new Vector<long>(array);
var span = array.AsSpan(vectorSize);
while (span.Length >= vectorSize)
vectorSum += new Vector<long>(span);
span = span.Slice(vectorSize);
return Vector.Dot(vectorSum, Vector<long>.One);
public static long SumSimdParallel(long[] array)
var degreeOfParallelism = Math.Max(Vector<long>.Count, (int)BitOperations.RoundUpToPowerOf2((uint)Environment.ProcessorCount));
var sums = new long[degreeOfParallelism];
var len = array.Length / degreeOfParallelism;
Parallel.For(0, degreeOfParallelism, idx =>
var vectorSize = Vector<long>.Count;
var span = array.AsSpan(idx * len, len);
var vectorSum = new Vector<long>(span);
span = span.Slice(vectorSize);
while (span.Length >= vectorSize)
vectorSum += new Vector<long>(span);
span = span.Slice(vectorSize);
sums[idx] = Vector.Dot(vectorSum, Vector<long>.One);