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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
// |jit-test| slow;
// XXXbz I would dearly like to wrap it up into a function to avoid polluting
// the global scope, but the function ends up heavyweight, and then we lose on
// the jit.
load(libdir + "mandelbrot-results.js");
//function testMandelbrotAll() {
// Configuration options that affect which codepaths we follow.
var doImageData = true;
var avoidSparseArray = true;
// Control of iteration numbers and sizing. We'll do
// scaler * colorNames.length iterations or so before deciding that we
// don't escape.
const scaler = 5;
const numRows = 600;
const numCols = 600;
const colorNames = [
"black",
"green",
"blue",
"red",
"purple",
"orange",
"cyan",
"yellow",
"magenta",
"brown",
"pink",
"chartreuse",
"darkorange",
"crimson",
"gray",
"deeppink",
"firebrick",
"lavender",
"lawngreen",
"lightsalmon",
"lime",
"goldenrod"
];
const threshold = (colorNames.length - 1) * scaler;
// Now set up our colors
var colors = [];
// 3-part for loop (iterators buggy, we will add a separate test for them)
for (var colorNameIdx = 0; colorNameIdx < colorNames.length; ++colorNameIdx) {
//for (var colorNameIdx in colorNames) {
colorNameIdx = parseInt(colorNameIdx);
colors.push([colorNameIdx, colorNameIdx, colorNameIdx, 0]);
}
// Storage for our point data
var points;
var scratch = {};
var scratchZ = {};
function complexMult(a, b) {
var newr = a.r * b.r - a.i * b.i;
var newi = a.r * b.i + a.i * b.r;
scratch.r = newr;
scratch.i = newi;
return scratch;
}
function complexAdd(a, b) {
scratch.r = a.r + b.r;
scratch.i = a.i + b.i;
return scratch;
}
function abs(a) {
return Math.sqrt(a.r * a.r + a.i * a.i);
}
function escapeAbsDiff(normZ, absC) {
var absZ = Math.sqrt(normZ);
return normZ > absZ + absC;
}
function escapeNorm2(normZ) {
return normZ > 4;
}
function fuzzyColors(i) {
return Math.floor(i / scaler) + 1;
}
function moddedColors(i) {
return (i % (colorNames.length - 1)) + 1;
}
function computeEscapeSpeedObjects(real, imag) {
var c = { r: real, i: imag }
scratchZ.r = scratchZ.i = 0;
var absC = abs(c);
for (var i = 0; i < threshold; ++i) {
scratchZ = complexAdd(c, complexMult(scratchZ, scratchZ));
if (escape(scratchZ.r * scratchZ.r + scratchZ.i * scratchZ.i,
absC)) {
return colorMap(i);
}
}
return 0;
}
function computeEscapeSpeedOneObject(real, imag) {
// fold in the fact that we start with 0
var r = real;
var i = imag;
var absC = abs({r: real, i: imag});
for (var j = 0; j < threshold; ++j) {
var r2 = r * r;
var i2 = i * i;
if (escape(r2 + i2, absC)) {
return colorMap(j);
}
i = 2 * r * i + imag;
r = r2 - i2 + real;
}
return 0;
}
function computeEscapeSpeedDoubles(real, imag) {
// fold in the fact that we start with 0
var r = real;
var i = imag;
var absC = Math.sqrt(real * real + imag * imag);
for (var j = 0; j < threshold; ++j) {
var r2 = r * r;
var i2 = i * i;
if (escape(r2 + i2, absC)) {
return colorMap(j);
}
i = 2 * r * i + imag;
r = r2 - i2 + real;
}
return 0;
}
var computeEscapeSpeed = computeEscapeSpeedDoubles;
var escape = escapeNorm2;
var colorMap = fuzzyColors;
function addPointOrig(pointArray, n, i, j) {
if (!points[n]) {
points[n] = [];
points[n].push([i, j, 1, 1]);
} else {
var point = points[n][points[n].length-1];
if (point[0] == i && point[1] == j - point[3]) {
++point[3];
} else {
points[n].push([i, j, 1, 1]);
}
}
}
function addPointImagedata(pointArray, n, col, row) {
var slotIdx = ((row * numCols) + col) * 4;
pointArray[slotIdx] = colors[n][0];
pointArray[slotIdx+1] = colors[n][1];
pointArray[slotIdx+2] = colors[n][2];
pointArray[slotIdx+3] = colors[n][3];
}
function createMandelSet() {
var realRange = { min: -2.1, max: 1 };
var imagRange = { min: -1.5, max: 1.5 };
var addPoint;
if (doImageData) {
addPoint = addPointImagedata;
points = new Array(4*numCols*numRows);
if (avoidSparseArray) {
for (var idx = 0; idx < 4*numCols*numRows; ++idx) {
points[idx] = 0;
}
}
} else {
addPoint = addPointOrig;
points = [];
}
var realStep = (realRange.max - realRange.min)/numCols;
var imagStep = (imagRange.min - imagRange.max)/numRows;
for (var i = 0, curReal = realRange.min;
i < numCols;
++i, curReal += realStep) {
for (var j = 0, curImag = imagRange.max;
j < numRows;
++j, curImag += imagStep) {
var n = computeEscapeSpeed(curReal, curImag);
addPoint(points, n, i, j)
}
}
var result;
if (doImageData) {
if (colorMap == fuzzyColors) {
result = mandelbrotImageDataFuzzyResult;
} else {
result = mandelbrotImageDataModdedResult;
}
} else {
result = mandelbrotNoImageDataResult;
}
return points.toSource() == result;
}
const escapeTests = [ escapeAbsDiff ];
const colorMaps = [ fuzzyColors, moddedColors ];
const escapeComputations = [ computeEscapeSpeedObjects,
computeEscapeSpeedOneObject,
computeEscapeSpeedDoubles ];
// Test all possible escape-speed generation codepaths, using the
// imageData + sparse array avoidance storage.
doImageData = true;
avoidSparseArray = true;
for (var escapeIdx in escapeTests) {
escape = escapeTests[escapeIdx];
for (var colorMapIdx in colorMaps) {
colorMap = colorMaps[colorMapIdx];
for (var escapeComputationIdx in escapeComputations) {
computeEscapeSpeed = escapeComputations[escapeComputationIdx];
assertEq(createMandelSet(), true);
}
}
}
// Test all possible storage strategies. Note that we already tested
// doImageData == true with avoidSparseArray == true.
escape = escapeAbsDiff;
colorMap = fuzzyColors; // This part doesn't really matter too much here
computeEscapeSpeed = computeEscapeSpeedDoubles;
doImageData = true;
avoidSparseArray = false;
assertEq(createMandelSet(), true);
escape = escapeNorm2;
doImageData = false; // avoidSparseArray doesn't matter here
assertEq(createMandelSet(), true);
//}
//testMandelbrotAll();
|