shithub: mc

Download patch

ref: 093dd263b29bd0014d4869d7bca8520cc382952e
parent: 36a26aa671237ac768d99d0a30edc35fd10dffd0
author: Ori Bernstein <[email protected]>
date: Thu Sep 18 12:38:52 EDT 2014

Add mandelbrot benchmark.

--- a/bench/Makefile
+++ b/bench/Makefile
@@ -4,6 +4,7 @@
 	 copious-allocs.myr \
 	 sha1-compute.myr \
 	 bigfactorial.myr \
+	 mandelbrot.myr 
 
 include ../config.mk
 include ../mk/c.mk
--- /dev/null
+++ b/bench/mandelbrot.myr
@@ -1,0 +1,54 @@
+use std
+use bio
+
+const Bailout : flt64 = 16.0
+const Maxiter = 1000
+
+const mandelbrot = {x, y
+	var cr, ci, zr, zi
+	var tmp, zr2, zi2
+	var i : int
+
+	cr = y - 0.5
+	ci = x
+	zr = 0.0
+	zi = 0.0
+
+	i = 0
+
+	while true
+		i++
+		tmp  = zr * zi
+		zr2 = zr * zr
+		zi2 = zi * zi
+		zr = zr2 - zi2 + cr
+		zi = tmp + tmp + ci
+		if zi2 + zr2 > Bailout
+			-> i
+		;;
+		if i > Maxiter
+			-> 0
+		;;
+	;;
+}
+
+const main = {args : byte[:][:]
+	var x : flt64, y : flt64, i
+	var f
+
+	f = bio.mkfile(1, bio.Wr)
+	for i = 0; i < 10; i++
+		for y = -39.0; y < 39.0; y = y + 1.0
+			for x = -39.0; x < 39.0; x = x + 1.0
+				if mandelbrot(x/40.0, y/40.0) == 0
+					bio.write(f, "*")
+				else
+					bio.write(f, " ")
+				;;
+			;;
+			bio.write(f, "\n")
+		;;
+	;;
+	bio.write(f, "\n")
+	bio.close(f)
+}
--- a/bench/runner.c
+++ b/bench/runner.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include <fcntl.h>
 #include <unistd.h>
 #include <err.h>
 #include <sys/types.h>
@@ -15,6 +16,7 @@
 {
     struct rusage ru;
     double sec, usec;
+    int in, out;
     char *cmd[2];
     int pid;
     int status;
@@ -25,6 +27,15 @@
     if (pid < 0) {
         err(1, "Could not fork\n");
     } else if (pid == 0) {
+        if ((in = open("/dev/zero", O_RDONLY)) < 0)
+            err(1, "could not open /dev/zero");
+        if ((out = open("/dev/null", O_WRONLY)) < 0)
+            err(1, "could not open /dev/null");
+        if (dup2(in, 0) < 0)
+            err(1, "could not reopen stdin");
+        if (dup2(out, 1) < 0)
+            err(1, "could not reopen stdout");
+
         cmd[0] = prog;
         cmd[1] = NULL;
         execv(prog, cmd);