-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy path3.cs
147 lines (129 loc) · 3.53 KB
/
3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public class MandelBrot
{
internal unsafe struct F64x8
{
internal fixed double buffer[8];
public F64x8() { }
public F64x8(double value)
{
for (var i = 0; i < 8; i++)
{
buffer[i] = value;
}
}
public double Get(int i)
{
return buffer[i];
}
public void Set(int i, double v)
{
buffer[i] = v;
}
public static void Add(double* a, double* b, double* r)
{
for (var i = 0; i < 8; i++)
{
r[i] = a[i] + b[i];
}
}
public static void Minus(double* a, double* b, double* r)
{
for (var i = 0; i < 8; i++)
{
r[i] = a[i] - b[i];
}
}
public static void Mul(double* a, double* b, double* r)
{
for (var i = 0; i < 8; i++)
{
r[i] = a[i] * b[i];
}
}
}
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;
var xloc = new F64x8[chunkSize];
for (var i = 0; i < chunkSize; i++)
{
var array = new F64x8();
var offset = i * 8;
for (var j = 0; j < 8; j++)
{
array.Set(j, (offset + j) * inv - 1.5);
}
xloc[i] = array;
}
Console.WriteLine($"P4\n{size} {size}");
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(Convert.ToHexStringLower(hash));
}
unsafe static byte mbrot8(F64x8 cr, double civ)
{
var ci = new F64x8(civ);
F64x8 zr;
F64x8 zi;
F64x8 tr;
F64x8 ti;
F64x8 absz;
F64x8 tmp;
for (var _i = 0; _i < 10; _i++)
{
for (var _j = 0; _j < 5; _j++)
{
F64x8.Add(zr.buffer, zr.buffer, tmp.buffer);
F64x8.Mul(tmp.buffer, zi.buffer, tmp.buffer);
F64x8.Add(tmp.buffer, ci.buffer, zi.buffer);
F64x8.Minus(tr.buffer, ti.buffer, tmp.buffer);
F64x8.Add(tmp.buffer, cr.buffer, zr.buffer);
F64x8.Mul(zr.buffer, zr.buffer, tr.buffer);
F64x8.Mul(zi.buffer, zi.buffer, ti.buffer);
}
F64x8.Add(tr.buffer, ti.buffer, absz.buffer);
var terminate = true;
for (var i = 0; i < 8; i++)
{
if (absz.Get(i) <= 4.0)
{
terminate = false;
break;
}
}
if (terminate)
{
return 0;
}
}
var accu = (byte)0;
for (var i = 0; i < 8; i++)
{
if (absz.Get(i) <= 4.0)
{
accu |= (byte)(0x80 >> i);
}
}
return accu;
}
}