Create a gist now

Instantly share code, notes, and snippets.

fractalModule =function(stdlib){
"use asm";
var pow = stdlib.Math.pow;
var abs = stdlib.Math.abs;
var atan2 = stdlib.Math.atan2;
var cos = stdlib.Math.cos;
var sin = stdlib.Math.sin;
function mandlebrot(cx, cy, maxIter) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
while ( (((x*x) + (y*y)) < 4.0)&((iter|0) < (maxIter|0))) {
xn = +( (x*x) - (y*y) + cx);
y = +((x*y)*2.0 + cy);
x = +xn;
iter=(iter+1)|0;
}
return iter|0;
}
function burningShip(cx, cy, maxIter) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
while ( (((x*x) + (y*y)) < 4.0)&((iter|0) < (maxIter|0))) {
xn = (x*x) - (y*y) - cx;
y = abs(x*y)*2.0 + cy;
x = xn;
iter=(iter+1)|0;
}
return iter|0;
}
function multibrot(cx, cy, maxIter, cr) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
cr = +cr;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
var n1 = 0.0;
var n2 = 0.0;
var n3 = 0.0;
n3 = +((x*x)+(y*y));
while (+n3 < 4.0 & (iter|0) < (maxIter|0)) {
if(n3==0.0){
x=+cx;
y=+cy;
n3 = +((x*x)+(y*y));
continue;
}
n1=pow(n3,(cr/1.0));
n2=cr*atan2(y,x);
xn=n1*cos(n2) + cx;
y=n1*sin(n2) + cy;
x = xn;
n3=((x*x)+(y*y));
iter=(iter+1)|0;
}
return iter|0;
}
function multibrot3(cx, cy, maxIter) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
while ( (((x*x) + (y*y)) < 4.0)&((iter|0) < (maxIter|0))) {
xn=pow(x,3.0)-3.0*x*(y*y) + cx;
y=3.0*(x*x)*y-pow(y,3.0) + cy;
x = xn;
iter=(iter+1)|0;
}
return iter|0;
}
function multibrot5(cx, cy, maxIter) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
while ( (((x*x) + (y*y)) < 4.0)&((iter|0) < (maxIter|0))) {
xn=pow(x,5.0)-(10.0*pow(x,3.0)*(y*y))+(5.0*x*pow(y,4.0)) + cx;
y=(5.0*pow(x,4.0)*y)-(10.0*(x*x)*pow(y,3.0))+pow(y,5.0) + cy;
x = xn;
iter=(iter+1)|0;
}
return iter|0;
}
function tricorn(cx, cy, maxIter) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
while ( (((x*x) + (y*y)) < 4.0)&((iter|0) < (maxIter|0))) {
xn = (x*x) - (y*y) - cx;
y =(x+x)*(-y) + cy;
x = xn;
iter=(iter+1)|0;
}
return iter|0;
}
function julia(cx, cy, maxIter, cr, ci) {
cx = +cx;
cy = +cy;
maxIter = maxIter|0;
cr = +cr;
ci = +ci;
var iter=0;
var xn= 0.0;
var x = 0.0;
var y = 0.0;
x = cx;
y = cy;
while ( (((x*x) + (y*y)) < 4.0)&((iter|0) < (maxIter|0))) {
xn = (x*x) - (y*y) + cr;
y = (x*y)*2.0 + ci;
x = xn;
iter=(iter+1)|0;
}
return iter|0;
}
return {
mandlebrot: mandlebrot,
burningShip: burningShip,
multibrot: multibrot,
multibrot3: multibrot3,
multibrot5: multibrot5,
tricorn: tricorn,
julia: julia
};
};
fractalFunctions=fractalModule(self);
@louisremi

Hi, have you got a benchmark of this with and without the "use asm" instruction? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment