Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // |jit-test| slow; |
michael@0 | 2 | |
michael@0 | 3 | // XXXbz I would dearly like to wrap it up into a function to avoid polluting |
michael@0 | 4 | // the global scope, but the function ends up heavyweight, and then we lose on |
michael@0 | 5 | // the jit. |
michael@0 | 6 | load(libdir + "mandelbrot-results.js"); |
michael@0 | 7 | //function testMandelbrotAll() { |
michael@0 | 8 | // Configuration options that affect which codepaths we follow. |
michael@0 | 9 | var doImageData = true; |
michael@0 | 10 | var avoidSparseArray = true; |
michael@0 | 11 | |
michael@0 | 12 | // Control of iteration numbers and sizing. We'll do |
michael@0 | 13 | // scaler * colorNames.length iterations or so before deciding that we |
michael@0 | 14 | // don't escape. |
michael@0 | 15 | const scaler = 5; |
michael@0 | 16 | const numRows = 600; |
michael@0 | 17 | const numCols = 600; |
michael@0 | 18 | |
michael@0 | 19 | const colorNames = [ |
michael@0 | 20 | "black", |
michael@0 | 21 | "green", |
michael@0 | 22 | "blue", |
michael@0 | 23 | "red", |
michael@0 | 24 | "purple", |
michael@0 | 25 | "orange", |
michael@0 | 26 | "cyan", |
michael@0 | 27 | "yellow", |
michael@0 | 28 | "magenta", |
michael@0 | 29 | "brown", |
michael@0 | 30 | "pink", |
michael@0 | 31 | "chartreuse", |
michael@0 | 32 | "darkorange", |
michael@0 | 33 | "crimson", |
michael@0 | 34 | "gray", |
michael@0 | 35 | "deeppink", |
michael@0 | 36 | "firebrick", |
michael@0 | 37 | "lavender", |
michael@0 | 38 | "lawngreen", |
michael@0 | 39 | "lightsalmon", |
michael@0 | 40 | "lime", |
michael@0 | 41 | "goldenrod" |
michael@0 | 42 | ]; |
michael@0 | 43 | const threshold = (colorNames.length - 1) * scaler; |
michael@0 | 44 | |
michael@0 | 45 | // Now set up our colors |
michael@0 | 46 | var colors = []; |
michael@0 | 47 | // 3-part for loop (iterators buggy, we will add a separate test for them) |
michael@0 | 48 | for (var colorNameIdx = 0; colorNameIdx < colorNames.length; ++colorNameIdx) { |
michael@0 | 49 | //for (var colorNameIdx in colorNames) { |
michael@0 | 50 | colorNameIdx = parseInt(colorNameIdx); |
michael@0 | 51 | colors.push([colorNameIdx, colorNameIdx, colorNameIdx, 0]); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | // Storage for our point data |
michael@0 | 55 | var points; |
michael@0 | 56 | |
michael@0 | 57 | var scratch = {}; |
michael@0 | 58 | var scratchZ = {}; |
michael@0 | 59 | function complexMult(a, b) { |
michael@0 | 60 | var newr = a.r * b.r - a.i * b.i; |
michael@0 | 61 | var newi = a.r * b.i + a.i * b.r; |
michael@0 | 62 | scratch.r = newr; |
michael@0 | 63 | scratch.i = newi; |
michael@0 | 64 | return scratch; |
michael@0 | 65 | } |
michael@0 | 66 | function complexAdd(a, b) { |
michael@0 | 67 | scratch.r = a.r + b.r; |
michael@0 | 68 | scratch.i = a.i + b.i; |
michael@0 | 69 | return scratch; |
michael@0 | 70 | } |
michael@0 | 71 | function abs(a) { |
michael@0 | 72 | return Math.sqrt(a.r * a.r + a.i * a.i); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function escapeAbsDiff(normZ, absC) { |
michael@0 | 76 | var absZ = Math.sqrt(normZ); |
michael@0 | 77 | return normZ > absZ + absC; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function escapeNorm2(normZ) { |
michael@0 | 81 | return normZ > 4; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | function fuzzyColors(i) { |
michael@0 | 85 | return Math.floor(i / scaler) + 1; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function moddedColors(i) { |
michael@0 | 89 | return (i % (colorNames.length - 1)) + 1; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | function computeEscapeSpeedObjects(real, imag) { |
michael@0 | 93 | var c = { r: real, i: imag } |
michael@0 | 94 | scratchZ.r = scratchZ.i = 0; |
michael@0 | 95 | var absC = abs(c); |
michael@0 | 96 | for (var i = 0; i < threshold; ++i) { |
michael@0 | 97 | scratchZ = complexAdd(c, complexMult(scratchZ, scratchZ)); |
michael@0 | 98 | if (escape(scratchZ.r * scratchZ.r + scratchZ.i * scratchZ.i, |
michael@0 | 99 | absC)) { |
michael@0 | 100 | return colorMap(i); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | return 0; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function computeEscapeSpeedOneObject(real, imag) { |
michael@0 | 107 | // fold in the fact that we start with 0 |
michael@0 | 108 | var r = real; |
michael@0 | 109 | var i = imag; |
michael@0 | 110 | var absC = abs({r: real, i: imag}); |
michael@0 | 111 | for (var j = 0; j < threshold; ++j) { |
michael@0 | 112 | var r2 = r * r; |
michael@0 | 113 | var i2 = i * i; |
michael@0 | 114 | if (escape(r2 + i2, absC)) { |
michael@0 | 115 | return colorMap(j); |
michael@0 | 116 | } |
michael@0 | 117 | i = 2 * r * i + imag; |
michael@0 | 118 | r = r2 - i2 + real; |
michael@0 | 119 | } |
michael@0 | 120 | return 0; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | function computeEscapeSpeedDoubles(real, imag) { |
michael@0 | 124 | // fold in the fact that we start with 0 |
michael@0 | 125 | var r = real; |
michael@0 | 126 | var i = imag; |
michael@0 | 127 | var absC = Math.sqrt(real * real + imag * imag); |
michael@0 | 128 | for (var j = 0; j < threshold; ++j) { |
michael@0 | 129 | var r2 = r * r; |
michael@0 | 130 | var i2 = i * i; |
michael@0 | 131 | if (escape(r2 + i2, absC)) { |
michael@0 | 132 | return colorMap(j); |
michael@0 | 133 | } |
michael@0 | 134 | i = 2 * r * i + imag; |
michael@0 | 135 | r = r2 - i2 + real; |
michael@0 | 136 | } |
michael@0 | 137 | return 0; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | var computeEscapeSpeed = computeEscapeSpeedDoubles; |
michael@0 | 141 | var escape = escapeNorm2; |
michael@0 | 142 | var colorMap = fuzzyColors; |
michael@0 | 143 | |
michael@0 | 144 | function addPointOrig(pointArray, n, i, j) { |
michael@0 | 145 | if (!points[n]) { |
michael@0 | 146 | points[n] = []; |
michael@0 | 147 | points[n].push([i, j, 1, 1]); |
michael@0 | 148 | } else { |
michael@0 | 149 | var point = points[n][points[n].length-1]; |
michael@0 | 150 | if (point[0] == i && point[1] == j - point[3]) { |
michael@0 | 151 | ++point[3]; |
michael@0 | 152 | } else { |
michael@0 | 153 | points[n].push([i, j, 1, 1]); |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | function addPointImagedata(pointArray, n, col, row) { |
michael@0 | 159 | var slotIdx = ((row * numCols) + col) * 4; |
michael@0 | 160 | pointArray[slotIdx] = colors[n][0]; |
michael@0 | 161 | pointArray[slotIdx+1] = colors[n][1]; |
michael@0 | 162 | pointArray[slotIdx+2] = colors[n][2]; |
michael@0 | 163 | pointArray[slotIdx+3] = colors[n][3]; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | function createMandelSet() { |
michael@0 | 167 | var realRange = { min: -2.1, max: 1 }; |
michael@0 | 168 | var imagRange = { min: -1.5, max: 1.5 }; |
michael@0 | 169 | |
michael@0 | 170 | var addPoint; |
michael@0 | 171 | if (doImageData) { |
michael@0 | 172 | addPoint = addPointImagedata; |
michael@0 | 173 | points = new Array(4*numCols*numRows); |
michael@0 | 174 | if (avoidSparseArray) { |
michael@0 | 175 | for (var idx = 0; idx < 4*numCols*numRows; ++idx) { |
michael@0 | 176 | points[idx] = 0; |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | } else { |
michael@0 | 180 | addPoint = addPointOrig; |
michael@0 | 181 | points = []; |
michael@0 | 182 | } |
michael@0 | 183 | var realStep = (realRange.max - realRange.min)/numCols; |
michael@0 | 184 | var imagStep = (imagRange.min - imagRange.max)/numRows; |
michael@0 | 185 | for (var i = 0, curReal = realRange.min; |
michael@0 | 186 | i < numCols; |
michael@0 | 187 | ++i, curReal += realStep) { |
michael@0 | 188 | for (var j = 0, curImag = imagRange.max; |
michael@0 | 189 | j < numRows; |
michael@0 | 190 | ++j, curImag += imagStep) { |
michael@0 | 191 | var n = computeEscapeSpeed(curReal, curImag); |
michael@0 | 192 | addPoint(points, n, i, j) |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | var result; |
michael@0 | 196 | if (doImageData) { |
michael@0 | 197 | if (colorMap == fuzzyColors) { |
michael@0 | 198 | result = mandelbrotImageDataFuzzyResult; |
michael@0 | 199 | } else { |
michael@0 | 200 | result = mandelbrotImageDataModdedResult; |
michael@0 | 201 | } |
michael@0 | 202 | } else { |
michael@0 | 203 | result = mandelbrotNoImageDataResult; |
michael@0 | 204 | } |
michael@0 | 205 | return points.toSource() == result; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | const escapeTests = [ escapeAbsDiff ]; |
michael@0 | 209 | const colorMaps = [ fuzzyColors, moddedColors ]; |
michael@0 | 210 | const escapeComputations = [ computeEscapeSpeedObjects, |
michael@0 | 211 | computeEscapeSpeedOneObject, |
michael@0 | 212 | computeEscapeSpeedDoubles ]; |
michael@0 | 213 | // Test all possible escape-speed generation codepaths, using the |
michael@0 | 214 | // imageData + sparse array avoidance storage. |
michael@0 | 215 | doImageData = true; |
michael@0 | 216 | avoidSparseArray = true; |
michael@0 | 217 | for (var escapeIdx in escapeTests) { |
michael@0 | 218 | escape = escapeTests[escapeIdx]; |
michael@0 | 219 | for (var colorMapIdx in colorMaps) { |
michael@0 | 220 | colorMap = colorMaps[colorMapIdx]; |
michael@0 | 221 | for (var escapeComputationIdx in escapeComputations) { |
michael@0 | 222 | computeEscapeSpeed = escapeComputations[escapeComputationIdx]; |
michael@0 | 223 | assertEq(createMandelSet(), true); |
michael@0 | 224 | } |
michael@0 | 225 | } |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | // Test all possible storage strategies. Note that we already tested |
michael@0 | 229 | // doImageData == true with avoidSparseArray == true. |
michael@0 | 230 | escape = escapeAbsDiff; |
michael@0 | 231 | colorMap = fuzzyColors; // This part doesn't really matter too much here |
michael@0 | 232 | computeEscapeSpeed = computeEscapeSpeedDoubles; |
michael@0 | 233 | |
michael@0 | 234 | doImageData = true; |
michael@0 | 235 | avoidSparseArray = false; |
michael@0 | 236 | assertEq(createMandelSet(), true); |
michael@0 | 237 | |
michael@0 | 238 | escape = escapeNorm2; |
michael@0 | 239 | doImageData = false; // avoidSparseArray doesn't matter here |
michael@0 | 240 | assertEq(createMandelSet(), true); |
michael@0 | 241 | //} |
michael@0 | 242 | //testMandelbrotAll(); |