Skip to content

Instantly share code, notes, and snippets.

@airbreather
Last active January 27, 2018 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airbreather/0a2493247607fe9b663cf22568c219a3 to your computer and use it in GitHub Desktop.
Save airbreather/0a2493247607fe9b663cf22568c219a3 to your computer and use it in GitHub Desktop.
using System;
using System.Numerics;
// <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
using static System.Runtime.CompilerServices.Unsafe;
namespace TheAbyss
{
class Program
{
static unsafe void Main(string[] args)
{
Random random = new Random();
int vectorCount = 4;
int scalarCount = vectorCount * Vector<int>.Count;
int* data = stackalloc int[scalarCount];
// init
for (int i = 0; i < scalarCount; i++)
{
Console.Write((data[i] = random.Next(100)).ToString().PadLeft(3) + " ");
}
Console.WriteLine();
Console.WriteLine();
int add = random.Next(100);
Console.WriteLine($"+ {add}");
Console.WriteLine();
Vector<int> addVector = new Vector<int>(add);
// use
for (int* curr = data, end = data + scalarCount; curr < end; curr += Vector<int>.Count)
{
WriteUnaligned(curr, ReadUnaligned<Vector<int>>(curr) + addVector);
}
for (int i = 0; i < scalarCount; i++)
{
Console.Write(data[i].ToString().PadLeft(3) + " ");
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment