Skip to content

Commit fcf02c6

Browse files
Add SIMD CSharp Mandlebrot (#428)
if rust gets SIMD so do we Co-authored-by: hanabi1224 <[email protected]>
1 parent 0a30fb0 commit fcf02c6

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

bench/algorithm/mandelbrot/4.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.Intrinsics;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
using System;
6+
7+
public class MandelBrot
8+
{
9+
public static void Main(string[] args)
10+
{
11+
var size = args.Length == 0 ? 200 : int.Parse(args[0]);
12+
size = (size + 7) / 8 * 8;
13+
var chunkSize = size / 8;
14+
var inv = 2.0 / size;
15+
Console.WriteLine($"P4\n{size} {size}");
16+
17+
var xloc = new Vector512<double>[chunkSize];
18+
for (var i = 0; i < chunkSize; i++)
19+
{
20+
var array = new double[8];
21+
var offset = i * 8;
22+
for (var j = 0; j < 8; j++)
23+
{
24+
array[j] = (offset + j) * inv - 1.5;
25+
}
26+
xloc[i] = Vector512.Create(array);
27+
}
28+
29+
var data = new byte[size * chunkSize];
30+
31+
for (var y = 0; y < size; y++)
32+
{
33+
var ci = y * inv - 1.0;
34+
for (var x = 0; x < chunkSize; x++)
35+
{
36+
var r = mbrot8(xloc[x], ci);
37+
if (r > 0)
38+
{
39+
data[y * chunkSize + x] = r;
40+
}
41+
}
42+
}
43+
44+
using var hasher = MD5.Create();
45+
var hash = hasher.ComputeHash(data);
46+
Console.WriteLine(ToHexString(hash));
47+
}
48+
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
static byte mbrot8(Vector512<double> cr, double civ)
51+
{
52+
Vector512<double> ci = Vector512.Create(civ);
53+
Vector512<double> zr = Vector512.Create(0.0);
54+
Vector512<double> zi = Vector512.Create(0.0);
55+
Vector512<double> tr = Vector512.Create(0.0);
56+
Vector512<double> ti = Vector512.Create(0.0);
57+
Vector512<double> absz = Vector512.Create(0.0);
58+
59+
for (var _i = 0; _i < 10; _i++)
60+
{
61+
for (var _j = 0; _j < 5; _j++)
62+
{
63+
zi = (zr + zr) * zi + ci;
64+
zr = tr - ti + cr;
65+
tr = zr * zr;
66+
ti = zi * zi;
67+
}
68+
absz = tr + ti;
69+
var terminate = true;
70+
for (var i = 0; i < 8; i++)
71+
{
72+
if (absz[i] <= 4.0)
73+
{
74+
terminate = false;
75+
break;
76+
}
77+
}
78+
if (terminate)
79+
{
80+
return 0;
81+
}
82+
}
83+
84+
var accu = (byte)0;
85+
for (var i = 0; i < 8; i++)
86+
{
87+
if (absz[i] <= 4.0)
88+
{
89+
accu |= (byte)(0x80 >> i);
90+
}
91+
}
92+
return accu;
93+
}
94+
95+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96+
static string ToHexString(byte[] ba)
97+
{
98+
StringBuilder hex = new StringBuilder(ba.Length * 2);
99+
foreach (byte b in ba)
100+
{
101+
hex.AppendFormat("{0:x2}", b);
102+
}
103+
return hex.ToString();
104+
}
105+
}

bench/bench_csharp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ problems:
4141
- 1.cs
4242
- 2.cs
4343
- 3.cs
44+
- 4.cs
4445
- name: json-serde
4546
source:
4647
- 1.cs

0 commit comments

Comments
 (0)