Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
this is benchmark of Mandelbrot set in JS, compiled JS and inline C. compiler output inside comments can be used instead of code above it, if you don't have EspruinoCompiler enabled
function mandelCompJS(x,y) {
"compiled";
var Xr = 0;
var Xi = 0;
var i = 0;
var Cr=(4*x/64)-2;
var Ci=(4*y/64)-2;
while ((i<32) & ((Xr*Xr+Xi*Xi)<4)) {
var t=Xr*Xr - Xi*Xi + Cr;
Xi=2*Xr*Xi+Ci;
Xr=t;
i++;
}
return i;
}
/* compiler output, can be used directly
var mandelCompJS = E.nativeCall(1, "JsVar(JsVar,JsVar)", atob("LenwTwZGhbAAIItGAPAA+QRGACAA8Pz4BUYEIADw+PgxRioigkYA8Pn4BkZAIADw7/gvIgFGB0YwRgDw7/iARgIgAPDl+C0iAUaBRkBGAPDl+ACQSEYA8Of4QEYA8OT4OEYA8OH4MEYA8N74UEYA8Nv4BCAA8Mz4KiJZRoJGAPDN+AZGQCAA8MP4LyIBRgdGMEYA8MP4gEYCIADwufgtIgFGgUZARgDwufgBkEhGAPC7+EBGAPC4+DhGAPC1+DBGAPCy+FBGAPCv+FBPACYqIiFGIEYA8KL4KiKARilGKEYA8Jz4KyIBRoFGQEYA8Jb4gkYEIADwjPg8IgFGg0ZQRgDwjPj7agOQmEcDmh8uzL8AIwDwAQMQRgKTAPCF+FhGAPCC+FBGAPB/+EhGAPB8+EBGAPB5+AKbACtS0CFGKiIgRgDwa/gpRoFGKiIoRgDwZfgtIoJGAUZIRgDwX/gAmSsig0YA8Fr4gEZYRgDwXPhQRgDwWfhIRgDwVvgCIADwR/ghRioigkYA8Ej4KUYqIoNGAPBD+AGZApArIgDwPviBRihGAPBA+DpoSEaQRwVGSEYA8Dn4ApsYRgDwNfhYRgDwMvhQRgDwL/ggRgDwLPg7aEBGmEcERkBGATYA8CT4decwRgDwFPgGRgGYAPAc+ACYAPAZ+ChGAPAW+CBGAPAT+DBGBbC96PCPAL/gJgAgAUubaRhHAL/gJgAgAUvbaBhHAL/gJgAgAUtbaBhHAL/gJgAg"));
*/
function mandelJS(x,y) {
var Xr = 0;
var Xi = 0;
var i = 0;
var Cr=(4*x/64)-2;
var Ci=(4*y/64)-2;
while ((i<32) & ((Xr*Xr+Xi*Xi)<4)) {
var t=Xr*Xr - Xi*Xi + Cr;
Xi=2*Xr*Xi+Ci;
Xr=t;
i=i+1;
}
return i;
}
var c = E.compiledC(`
// int mandel(int, int)
// int runmandel()
int mandel(int x,int y){
float Xr = 0.0;
float Xi = 0.0;
int i = 0;
float Cr=(4*x/64.0)-2;
float Ci=(4*y/64.0)-2;
while ((i<32) & ((Xr*Xr+Xi*Xi)<4.0)) {
float t=Xr*Xr - Xi*Xi + Cr;
Xi=2.0*Xr*Xi+Ci;
Xr=t;
i=i+1;
}
return i;
}
int runmandel(){
int n=0;
for (int y=0;y<64;y++) {
for (int x=0;x<64;x++)
n+=mandel(x,y);
}
return n;
}
`);
/* compiler output, can be used directly
var c = (function(){
var bin=atob("gAAH7pAK3+0barju53r47gB6sO5nWokAp+4mWgAgB+4QGrjux3rx7gBK5+4mep/tEnqw7mdq8O5Hemfup2pn7gdaNu6lSrTuZErx7hD6D9UfKA3cd+6nerDuRkqn7odKATB27uV6sO5EenfuhXrk53BHAL8AAIA8AAAAAAAjELUcRgAiEEYZRv/3uv8BMkAqBET30QEzQCvz0SBGEL0AAA==");
return {
mandel:E.nativeCall(1, "int(int, int)", bin),
runmandel:E.nativeCall(125, "int()", bin),
};
})()
*/
function showmandel(f){
var x,y;
for (y=0;y<64;y+=1.0) {
line="";
for (x=0;x<64;x+=1.0)
line += " *"[f(x,y)&1];
print(line);
}
}
function runmandel(f){
var x,y;
for (y=0;y<64;y++) {
for (x=0;x<64;x++) f(x,y);
}
}
// benchmark starts here
//var d=Date();runmandel(mandelJS);d=Date().ms-d.ms;
//var d=Date();runmandel(mandelCompJS);d=Date().ms-d.ms;
//var d=Date();runmandel(c.mandel);d=Date().ms-d.ms;
//var d=Date();c.runmandel();d=Date().ms-d.ms;
//showmandel(c.mandel)
var fpu = E.compiledC(`
// int getFPSCR()
// int getPendingFPUIRQ()
// void clearFPUIRQFlags()
typedef unsigned int uint32_t;
typedef signed int int32_t;
__attribute__( ( always_inline ) ) static inline uint32_t __get_FPSCR(void)
{
uint32_t result;
__asm volatile ("");
__asm volatile ("VMRS %0, fpscr" : "=r" (result) );
__asm volatile ("");
return(result);
}
__attribute__( ( always_inline ) ) static inline void __set_FPSCR(uint32_t fpscr)
{
__asm volatile ("");
__asm volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc");
__asm volatile ("");
}
typedef struct {
volatile uint32_t ISER[8]; uint32_t RESERVED0[24];
volatile uint32_t ICER[8]; uint32_t RESERVED1[24];
volatile uint32_t ISPR[8]; uint32_t RESERVED2[24];
volatile uint32_t ICPR[8]; uint32_t RESERVED3[24];
} NVIC_Type;
__attribute__( ( always_inline ) ) static inline NVIC_Type *NVIC(){return ((NVIC_Type *)(0xE000E000UL+0x0100UL));}
typedef enum {
//removed all except fpu
FPU_IRQn = 38
} IRQn_Type;
static inline uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn)
{
return((uint32_t)(((NVIC()->ISPR[(((uint32_t)(int32_t)IRQn) >> 5UL)] & (1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL))) != 0UL) ? 1UL : 0UL));
}
static inline void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
{
NVIC()->ICPR[(((uint32_t)(int32_t)IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
}
int getPendingFPUIRQ(){
return NVIC_GetPendingIRQ(FPU_IRQn);
}
int getFPSCR(){
return __get_FPSCR();
}
void clearFPUIRQFlags(){
if(NVIC_GetPendingIRQ(FPU_IRQn)){
__set_FPSCR(__get_FPSCR() & ~(0x9FUL));
(void) __get_FPSCR();
NVIC_ClearPendingIRQ(FPU_IRQn);
}
}
`);
/* compiler output, can be used directly
var fpu = (function(){
var bin=atob("CErS+AQxWwYK1fHuEDoj8J8D4e4QOvHuEDpAI8L4hDFwRwC/AOEA4PHuEApwRwAAAkvT+AQBwPOAEHBHAOEA4A==");
return {
getFPSCR:E.nativeCall(41, "int()", bin),
getPendingFPUIRQ:E.nativeCall(49, "int()", bin),
clearFPUIRQFlags:E.nativeCall(1, "void()", bin),
};
})()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment