-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMandelbrot.cs
More file actions
175 lines (165 loc) · 7.05 KB
/
Mandelbrot.cs
File metadata and controls
175 lines (165 loc) · 7.05 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Numerics;
namespace BenchMarkDebug
{
class Mandelbrot
{
private Vector2 _bitMapSize;
private int _maxIterations;
//private int[] _imageColorsBW;
private int _oneLine;
private float _minX, _minY, _maxX, _maxY;
private bool hasFloats, hasVectors;
public bool HasAllResults { get => hasFloats & hasVectors; }
public Mandelbrot()
{
_bitMapSize = new Vector2(512, 512);
_oneLine = (int)_bitMapSize.X;
_maxIterations = 1000;
//_imageColorsBW = new int[1024 * 1024];
_minX = -2;
_maxX = 1;
_minY = -1;
_maxY = 1;
hasFloats = hasVectors = false;
}
public int[] CalcFloat()
{
int[] _imageColorsBW = new int[(int)(_bitMapSize.X * _bitMapSize.Y)];
float dx = (_maxX - _minX) / _bitMapSize.X;
float dy = (_maxY - _minY) / _bitMapSize.Y;
float xTemp, x, y, x0, y0 = _minY;
for(int j = 0; j < _bitMapSize.Y; j++)
{
//y0 = _minY + j * dy;
//y = 0;
x0 = _minX;
int iter;
for(int i = 0; i < _bitMapSize.X; i++)
{
/*if(i == 46)
{
string deb = "stop";
}*/
x = y = 0;
iter = 0;
do
{
xTemp = x * x - y * y + x0;
y = 2.0f * x * y + y0;
x = xTemp;
iter++;
if(x * x + y * y > 4)
{
break;
}
}
while (iter < _maxIterations);
_imageColorsBW[j * _oneLine + i] = iter;
x0 += dx;// _minX + (i + 1) * dx;
}
y0 += dy;
}
hasFloats = true;
return _imageColorsBW;
}
public int[] CalcVectorFloat()
{
int[] _imageColorsBW = new int[(int)(_bitMapSize.X * _bitMapSize.Y)];
float dx = (_maxX - _minX) / _bitMapSize.X;
Vector<float> dyVec = new Vector<float>((_maxY - _minY) / _bitMapSize.Y);
Vector<float> xTemp, x, y;
//Vector<float> xBeg = new Vector<float>(_minX);
Vector<int> iter;
Vector<int> iterStep;
Vector<int> maxInterVec = new Vector<int>(_maxIterations);
Vector<float> Two = new Vector<float>(2.0f);
Vector<float> Four = new Vector<float>(4.0f);
int slots = Vector<float>.Count;
//float[] tmpArray = new float[slots];
float[] x0Array = new float[slots];
//bool anyGreater;
for (int m = 0; m < slots; m++)
{
x0Array[m] = _minX + m * dx;
//tmpArray[m] = (m + 1) * dx;
}
float[] xStepArray = new float[slots];
for(int m = 0; m < slots; m++)
{
}
Vector<float> x0Step = new Vector<float>(slots * dx);
Vector<float> dxVec = new Vector<float>(dx);
Vector<float> x0; // = new Vector<float>(x0Array);
Vector<float> x0Curr;
Vector<float> y0 = new Vector<float>(_minY);
Vector<float> y0Curr = y0;
Vector<float> squareDistance, prevSquareDistance;
Vector<int> blewLimit;
for (int j = 0; j < _bitMapSize.Y; j++)
{
y = Vector<float>.Zero;
//x0 = dxVec;
x0 = new Vector<float>(x0Array);
//bool allZero;
for (int i = 0; i < _bitMapSize.X; i += slots)
{
/*if( i == 80)
{
string dum = "stop";
}*/
y0Curr = y0;
x0Curr = x0;
x = y = Vector<float>.Zero;
iter = Vector<int>.Zero;
iterStep = Vector<int>.One;
prevSquareDistance = Vector<float>.Zero;
do
{
xTemp = x * x - y * y + x0Curr;
y = Two * x * y + y0Curr;
x = xTemp;
iter += iterStep;
squareDistance = x * x + y * y;
//prevSquareDistance += squareDistance;
blewLimit = Vector.GreaterThan(squareDistance, Four);
prevSquareDistance = System.Numerics.Vector.ConditionalSelect(blewLimit, squareDistance, prevSquareDistance);
//bail out fast if all blew the limit
if (Vector.GreaterThanAll(prevSquareDistance, Four))
{
break;
}
else
//if (Vector.GreaterThanAny(tempVec, Four))
{
//first zero out the sum vector at positions where we reached limit of interations
//blewLimit = Vector.GreaterThanOrEqual(iter, maxInterVec);
blewLimit = Vector.LessThan(iter, maxInterVec) & Vector.LessThanOrEqual(prevSquareDistance, Four);
iterStep = Vector.Abs(blewLimit);
//iterStep = Vector.LessThan(iter, maxInterVec) & Vector.LessThan(squareDistance, Four);
//iterStep = Vector.ConditionalSelect(blewLimit, Vector<int>.Zero, iterStep);
//blewLimit = Vector.GreaterThan(squareDistance, Four);
//now zero out those where the distance exceeded limit
//iterStep = Vector.ConditionalSelect(blewLimit, Vector<int>.Zero, iterStep);
x0Curr = Vector.ConditionalSelect(blewLimit, x0Curr, Vector<float>.Zero);
y0Curr = Vector.ConditionalSelect(blewLimit, y0Curr,Vector<float>.Zero);
x = Vector.ConditionalSelect(blewLimit, x, Vector<float>.Zero);
y = Vector.ConditionalSelect(blewLimit, y, Vector<float>.Zero);
}
//allZero = ();
if(Vector.EqualsAll(iterStep, Vector<int>.Zero))
{
break;
}
}
while (Vector.LessThanAny(iter, maxInterVec));
iter.CopyTo(_imageColorsBW, j * _oneLine + i);
//_imageColorsBW[j * _oneLine + i] = iter;
x0 += x0Step;// dxVec;
}
y0 += dyVec;
}
hasVectors = true;
return _imageColorsBW;
}
}
}