Skip to content

Commit

Permalink
If rust gets SIMD so do we
Browse files Browse the repository at this point in the history
  • Loading branch information
xentripetal committed Feb 13, 2024
1 parent f2172e6 commit 987e486
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
105 changes: 105 additions & 0 deletions bench/algorithm/mandelbrot/4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Security.Cryptography;
using System.Text;
using System;

public class MandelBrot
{
public static void Main(string[] args)
{
var size = args.Length == 0 ? 200 : int.Parse(args[0]);
size = (size + 7) / 8 * 8;
var chunkSize = size / 8;
var inv = 2.0 / size;
Console.WriteLine($"P4\n{size} {size}");

var xloc = new Vector512<double>[chunkSize];
for (var i = 0; i < chunkSize; i++)
{
var array = new double[8];
var offset = i * 8;
for (var j = 0; j < 8; j++)
{
array[j] = (offset + j) * inv - 1.5;
}
xloc[i] = Vector512.Create(array);
}

var data = new byte[size * chunkSize];

for (var y = 0; y < size; y++)
{
var ci = y * inv - 1.0;
for (var x = 0; x < chunkSize; x++)
{
var r = mbrot8(xloc[x], ci);
if (r > 0)
{
data[y * chunkSize + x] = r;
}
}
}

using var hasher = MD5.Create();
var hash = hasher.ComputeHash(data);
Console.WriteLine(ToHexString(hash));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static byte mbrot8(Vector512<double> cr, double civ)
{
Vector512<double> ci = Vector512.Create(civ);
Vector512<double> zr = Vector512.Create(0.0);
Vector512<double> zi = Vector512.Create(0.0);
Vector512<double> tr = Vector512.Create(0.0);
Vector512<double> ti = Vector512.Create(0.0);
Vector512<double> absz = Vector512.Create(0.0);

for (var _i = 0; _i < 10; _i++)
{
for (var _j = 0; _j < 5; _j++)
{
zi = (zr + zr) * zi + ci;
zr = tr - ti + cr;
tr = zr * zr;
ti = zi * zi;
}
absz = tr + ti;
var terminate = true;
for (var i = 0; i < 8; i++)
{
if (absz[i] <= 4.0)
{
terminate = false;
break;
}
}
if (terminate)
{
return 0;
}
}

var accu = (byte)0;
for (var i = 0; i < 8; i++)
{
if (absz[i] <= 4.0)
{
accu |= (byte)(0x80 >> i);
}
}
return accu;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static string ToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
}
1 change: 1 addition & 0 deletions bench/bench_csharp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ problems:
- 1.cs
- 2.cs
- 3.cs
- 4.cs
- name: json-serde
source:
- 1.cs
Expand Down

0 comments on commit 987e486

Please sign in to comment.