aboutsummaryrefslogtreecommitdiff
path: root/examples/mandel.zc
blob: b389d402d2bcf7200c9082eb110313d08615a462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace;
include rt;

// the mandelbrot set in 16.16 fixed point. a word is 64 bits, so the product
// fits and the point sits 16 bits down. the jit inlines it as a multiply and a shift.
enhance fmul(a, b) micro "a = mul a b; a = sar a 16";

const ONE = 65536, W = 72, H = 28, MAXIT = 40;
let shades = " .,:;-=+*%#@";

int escape(int cx, int cy) {
	int x = 0, y = 0, i = 0;
	while (i < MAXIT) {
		int xx = fmul(x, x), yy = fmul(y, y);
		if (xx + yy > 4 * ONE) return i;
		y = 2 * fmul(x, y) + cy;
		x = xx - yy + cx;
		i++;
	}
	return MAXIT;
}
void main() {
	int row = 0;
	while (row < H) {
		int col = 0, cy = (row * 2400 / (H - 1) - 1200) * ONE / 1000;
		while (col < W) {
			int cx = (col * 3200 / (W - 1) - 2200) * ONE / 1000, it = escape(cx, cy);
			rt.put_c(it == MAXIT ? '@' : shades[it * 11 / MAXIT]);
			col++;
		}
		rt.put_c(10);
		row++;
	}
	return;
}