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 | // Adapted from |
michael@0 | 2 | // |
michael@0 | 3 | // https://github.com/RiverTrail/RiverTrail/blob/master/examples/mandelbrot/mandelbrot.js |
michael@0 | 4 | // |
michael@0 | 5 | // which in turn is adapted from a WebCL implementation available at |
michael@0 | 6 | // |
michael@0 | 7 | // http://www.ibiblio.org/e-notes/webcl/mandelbrot.html |
michael@0 | 8 | |
michael@0 | 9 | load(libdir + "parallelarray-helpers.js"); |
michael@0 | 10 | |
michael@0 | 11 | var nc = 30, maxCol = nc*3; |
michael@0 | 12 | |
michael@0 | 13 | // this is the actual mandelbrot computation, ported to JavaScript |
michael@0 | 14 | // from the WebCL / OpenCL example at |
michael@0 | 15 | // http://www.ibiblio.org/e-notes/webcl/mandelbrot.html |
michael@0 | 16 | function computeSetByRow(x, y) { |
michael@0 | 17 | var Cr = (x - 256) / scale + 0.407476; |
michael@0 | 18 | var Ci = (y - 256) / scale + 0.234204; |
michael@0 | 19 | var I = 0, R = 0, I2 = 0, R2 = 0; |
michael@0 | 20 | var n = 0; |
michael@0 | 21 | while ((R2+I2 < 2.0) && (n < 512)) { |
michael@0 | 22 | I = (R+R)*I+Ci; |
michael@0 | 23 | R = R2-I2+Cr; |
michael@0 | 24 | R2 = R*R; |
michael@0 | 25 | I2 = I*I; |
michael@0 | 26 | n++; |
michael@0 | 27 | } |
michael@0 | 28 | return n; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | function computeSequentially() { |
michael@0 | 32 | result = []; |
michael@0 | 33 | for (var r = 0; r < rows; r++) { |
michael@0 | 34 | for (var c = 0; c < cols; c++) { |
michael@0 | 35 | result.push(computeSetByRow(r, c)); |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | return result; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | var scale = 10000*300; |
michael@0 | 42 | var rows = 4; |
michael@0 | 43 | var cols = 4; |
michael@0 | 44 | |
michael@0 | 45 | // check that we get correct result |
michael@0 | 46 | if (getBuildConfiguration().parallelJS) { |
michael@0 | 47 | var expected = computeSequentially(); |
michael@0 | 48 | assertParallelExecSucceeds( |
michael@0 | 49 | function (m) Array.buildPar(rows * cols, function (xy) { |
michael@0 | 50 | return computeSetByRow((xy/cols)|0,(xy%cols)) |
michael@0 | 51 | }, m), |
michael@0 | 52 | function (r) assertStructuralEq(expected, r)); |
michael@0 | 53 | } |