Last active
January 27, 2018 13:09
-
-
Save airbreather/0a2493247607fe9b663cf22568c219a3 to your computer and use it in GitHub Desktop.
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; | |
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